简体   繁体   English

从字段中以PHP显示星期几

[英]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 嗨,大家好Kinda卡住了我的PHP文件的1部分,我按我需要的方式进行了其他所有设置,仅此而已

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 我想将其显示为一天,例如02/02/2013将显示为星期日而不是整个日期

i have already have 我已经有

date_default_timezone_set('Europe/London');

You can use the DateTime class and format it. 您可以使用DateTime类并设置其格式。

$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_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 : 如果日期字符串未正确解析,则可以使用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); 并使用函数$dw = date( "w", $timestamp); (w will return 0 for sunday, to 6 for saturday) in php 4 (w将在周日返回0,在周六返回6,在周六返回6)

or function $dw = date("N", $timestamp); 或函数$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) (N会在PHP 5中返回1或星期一,星期天返回7,则要注意)(也请注意您要更改的日期)

compare your $dw with your element array and print what you want. 将$ dw与元素数组进行比较,然后打印所需的内容。 Satisfied? 满意?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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