Day of the week by php variable

Asked

Viewed 6,727 times

0

I want to take the date in full. The code below is getting right, 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"; ?>

2 answers

6

Alternatively, you can use the function strftime, since the locale is properly configured. See an example:

<?php

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

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

First, is configured the locale for pt. If successfully changed, 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, no zeros left;
  • %B returns the name of the month in full, and
  • %Y returns the year with four digits.

The exit would be:

sexta-feira,  1 de setembro de 2017

5


$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";
  • Perfect my noble !

  • Obg! I made an edition taking variavel_convertida and leaving everything just variavel.

  • 1

    It is interesting to note that exactly the same question code could be used only getdate(strtotime($data)). Take an example: https://ideone.com/YLRK4i

  • @Andersoncarloswoss It’s true. But see that the date format he wanted is with /.

  • 1

    @Andersoncarloswoss Really the code looks much better now. You were right, just change the variable $today.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.