Day of the week by variable php [closed]

0

I want to get the date in full. The code below is catching, however, the current date of the computer.

I would like it to be as I put the date coming from a variable type 01/09/2017.

<?php
$meses = array (1 => "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho", 7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => "Dezembro");
$diasdasemana = array (1 => "Segunda-Feira",2 => "Terça-Feira",3 => "Quarta-Feira",4 => "Quinta-Feira",5 => "Sexta-Feira",6 => "Sábado",0 => "Domingo");
 $hoje = getdate();
 $dia = $hoje["mday"];
 $mes = $hoje["mon"];
 $nomemes = $meses[$mes];
 $ano = $hoje["year"];
 $diadasemana = $hoje["wday"];
 $nomediadasemana = $diasdasemana[$diadasemana];
 echo "$nomediadasemana, $dia de $nomemes de $ano"; ?>
    
asked by anonymous 01.09.2017 / 00:26

2 answers

1
$meses = array (1 => "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho", 7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => "Dezembro");
$diasdasemana = array (1 => "Segunda-Feira",2 => "Terça-Feira",3 => "Quarta-Feira",4 => "Quinta-Feira",5 => "Sexta-Feira",6 => "Sábado",0 => "Domingo");

$variavel = "01/09/2017";
$variavel = str_replace('/','-',$variavel);

$hoje = getdate(strtotime($variavel));

$dia = $hoje["mday"];
$mes = $hoje["mon"];
$nomemes = $meses[$mes];
$ano = $hoje["year"];
$diadasemana = $hoje["wday"];
$nomediadasemana = $diasdasemana[$diadasemana];

echo "$nomediadasemana, $dia de $nomemes de $ano";
    
01.09.2017 / 01:01
4

As an alternative, you can use the strftime function as long as locale is properly configured. Here's an example:

<?php

if (setlocale(LC_TIME, 'pt')) {
    echo strftime("%A, %e de %B de %Y", strtotime("01-09-2017")), PHP_EOL;
}
  

As discussed, even in the other answer, there is a difference in PHP between using - and / as separator. If you use - , PHP will consider the format dd-mm-YYYY , but if you use / PHP will consider mm/dd/YYYY . See documentation , third note.

First, the locale is set to pt . If changed successfully, it displays the date represented by strtotime("01-09-2017") in format %A, %e de %B de %Y , where:

  • %A returns the day of the week in full;
  • %e returns the day in numeral, without leading zeros;
  • %B returns the name of the month in full, and
  • %Y returns the year with four digits.

The output would be:

sexta-feira,  1 de setembro de 2017
    
01.09.2017 / 01:45