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

时间:2022-10-21 10:14:59

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

php是否具有自动将日期转换为日期值的功能,其中Monday = 1,Tuesday = 2等。这样的事情

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

5 个解决方案

#1


56  

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

#2


13  

What about using idate()? idate()

那么使用idate()呢? IDATE()

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

#3


6  

$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()返回当前日期的数字。

#4


5  

The date function can return this if you specify the format correctly:

如果正确指定格式,则date函数可以返回此值:

$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代表)。

#5


1  

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

Where $dow is 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是(星期)的一天。请注意本地时间的近似方法,但是(双关语):星期日不是一周中的最后一天,而是第一天(0)。

#1


56  

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

#2


13  

What about using idate()? idate()

那么使用idate()呢? IDATE()

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

#3


6  

$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()返回当前日期的数字。

#4


5  

The date function can return this if you specify the format correctly:

如果正确指定格式,则date函数可以返回此值:

$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代表)。

#5


1  

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

Where $dow is 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是(星期)的一天。请注意本地时间的近似方法,但是(双关语):星期日不是一周中的最后一天,而是第一天(0)。