简体   繁体   中英

Show Day of The week in PHP from field

Hi Guys im Kinda stuck with 1 part of my PHP File, i have everything else setup working the way i need it, just this bit i am stuck on

i have the follow row setup

echo "<td align='left' valign='middle' bgcolor='" . $color . "'><font size='" . $fontsize . "'>" . $row['date'] . "</td>";

i want to show it as a Day, for Example 02/06/2013 will show as Sunday rather than the full date

i have already have

date_default_timezone_set('Europe/London');

You can use the DateTime class and format it.

$date = new DateTime($row['date']);
echo $date->format('d/m/Y'); // will print 02/06/2013
echo $date->format('l'); // will print a day like Sunday

Or just use the date function:

echo date('d/m/Y', $row['date']); // will print 02/06/2013
echo date('l', $row['date']); // will print a day like Sunday

Check the date manual for more info.

You can use date_format :

$date = new DateTime($row['date']);
echo $date->format('l');

In case you date string does not get parsed correctly, you can use DateTime::createFromFormat :

$date = DateTime::createFromFormat("m/d/Y", $row['date']);
echo $date->format('l');

使用DateTime::createFromFormat更安全

echo DateTime::createFromFormat("d/m/Y","02/06/2013")->format("l");

Just format your date correctly:-

$date = \DateTime::createFromFormat('d/m/Y', $row['date']);
echo "<td>" . $date->format('l') . "</td>";

You should have an array of the days name:

 $days = array(
                1 => 'Monday',
                2 => 'Tuesday',
                3 => 'Wednesday',
                4 => 'Thursday',
                5 => 'Friday',
                6 => 'Saturday',
                0 => 'Sunday');

and use function $dw = date( "w", $timestamp); (w will return 0 for sunday, to 6 for saturday) in php 4

or function $dw = date("N", $timestamp); ( N will return 1 or monday, to 7 for sunday) in php 5 ( pay attention at your array of days to change there too)

compare your $dw with your element array and print what you want. Satisfied?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM