php 星期几到天数(星期一 = 1,星期二 = 2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4961793/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:22:36  来源:igfitidea点击:

day of the week to day number (Monday = 1, Tuesday = 2)

phpdatetimedate

提问by zmol

Does php have a function to automatically convert dates to their day value, where Monday=1, Tuesday=2, etc. Something like this

php 是否具有自动将日期转换为其日期值的功能,其中星期一 = 1,星期二 = 2,等等。类似这样的

$daynum = func('wednesday'); //echos 3

回答by ceejayoz

$day_of_week = date('N', strtotime('Monday'));

回答by Damien Pirsy

What about using idate()? idate()

使用 idate() 怎么样?日期()

$integer = idate('w', $timestamp);

回答by adrianbanks

The datefunction can return this if you specify the format correctly:

日期函数可以返回这个,如果你正确地指定格式:

$daynum = date("w", strtotime("wednesday"));

will return 0 for Sunday through to 6 for Saturday.

将在周日返回 0 到周六返回 6。

An alternative format is:

另一种格式是:

$daynum = date("N", strtotime("wednesday"));

which will return 1 for Monday through to 7 for Sunday (this is the ISO-8601 represensation).

它将从星期一返回 1 到星期日返回 7(这是 ISO-8601 表示)。

回答by Wige

$day_number = date('N', $date);

This will return a 1 for Monday to 7 for Sunday, for the date that is stored in $date. Omitting the second argument will cause date() to return the number for the current day.

对于存储在 $date 中的日期,这将返回星期一的 1 到星期日的 7。省略第二个参数将导致 date() 返回当天的数字。

回答by Linus Kleen

$tm = localtime($timestamp, TRUE);
$dow = $tm['tm_wday'];

Where $dowis the day of (the) week. Be aware of the herectic approach of localtime, though (pun): Sunday is not the last day of the week, but the first (0).

$dow星期几在哪里。请注意localtime,虽然(双关语)的异端方法:星期日不是一周的最后一天,而是第一个 (0)。