如何使用 PHP 获取特定日期的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1385801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the day of a specific date with PHP
提问by noob
I want to get the day (Sunday, Monday,...) of October 22. How can I do that?
我想得到 10 月 22 日的那一天(星期日、星期一……)。我该怎么做?
回答by Pascal MARTIN
You can use the datefunction. I'm using strtotimeto get the timestamp to that day ; there are other solutions, like mktime, for instance.
您可以使用该date功能。我正在使用strtotime获取当天的时间戳;还有其他解决方案,例如mktime,。
For instance, with the 'D' modifier, for the textual representation in three letters :
例如,使用 'D' 修饰符,对于三个字母的文本表示:
$timestamp = strtotime('2009-10-22');
$day = date('D', $timestamp);
var_dump($day);
You will get :
你会得到 :
string 'Thu' (length=3)
And with the 'l' modifier, for the full textual representation :
并使用 'l' 修饰符,用于全文表示:
$day = date('l', $timestamp);
var_dump($day);
You get :
你得到 :
string 'Thursday' (length=8)
Or the 'w' modifier, to get to number of the day (0 to 6, 0 being sunday, and 6 being saturday):
或 'w' 修饰符,以获取当天的编号(0 到 6,0 为星期日,6 为星期六):
$day = date('w', $timestamp);
var_dump($day);
You'll obtain :
您将获得:
string '4' (length=1)
回答by Péter Simon
$date = '2014-02-25';
date('D', strtotime($date));
回答by Pushpendra Rathor
$datetime = DateTime::createFromFormat('Ymd', '20151102');
echo $datetime->format('D');
回答by niquole
$date = strtotime('2016-2-3');
$date = date('l', $date);
var_dump($date)
(i added format 'l' so it will return full name of day)
(我添加了格式 'l' 所以它会返回一天的全名)
回答by code_burgar
$date = '2009-10-22';
$sepparator = '-';
$parts = explode($sepparator, $date);
$dayForDate = date("l", mktime(0, 0, 0, $parts[1], $parts[2], $parts[0]));

