Convert a date to the name of the day of the week

8

I am receiving multiple dates entered by the user from the database (the field is of type data (yyyy-mm-dd)). What I want, is to grab on that date and convert it to the name of the week day. For example:

<?php
   $data_da_bd = "2014-08-13";
?>

The result would be: Wednesday.

    
asked by anonymous 20.08.2014 / 17:05

3 answers

6

PHP has facilities for this.

link

Just use the desired pattern (in your case, %A ) that the query will return correctly.

Do not forget to put Locale correct or will display in another language.

== EDIT ==

Since your date is in String, you must first convert it to time.

You can use the link

In this case, the end would be

date_default_timezone_set("Europe/Lisbon"); 
$data = "2014-08-09"; 
echo(strftime("%A",strtotime($data));
    
20.08.2014 / 17:07
5

An alternate way to work around the problem is to use the IntlDateFormatter class to manipulate the formatted of the date. EEEE in setPattern() means what the format of the date in case is the day in full, for other format options check documentation

<?php
date_default_timezone_set('America/Sao_Paulo');

$data = new DateTime('2012-03-20');
$formatter = new IntlDateFormatter('pt_BR', IntlDateFormatter::FULL, 
                                    IntlDateFormatter::FULL, 
                                    IntlDateFormatter::GREGORIAN);
$formatter->setPattern('EEEE');

echo $data->format('d/m/Y') .' é: '. $formatter->format($data);

Example

This response was based on: php how to format a given datetime object by taking locale get default

    
20.08.2014 / 20:16
4

Just complementing: you can do this using object orientation or procedurally, and Felipe's answer fit the latter case. To do this using objects (and the DateTime class of PHP), just do it as follows:

<?php
$data = new DateTime('2014-08-09', new DateTimeZone('Europe/Lisbon'));
echo $data->format('l');
    
20.08.2014 / 17:33