day of the week to day number (Monday = 1, Tuesday = 2)
Does php have a function to automati开发者_开发技巧cally convert dates to their day value, where Monday=1, Tuesday=2, etc. Something like this
$daynum = func('wednesday'); //echos 3
$day_of_week = date('N', strtotime('Monday'));
The date function can return this if you specify the format correctly:
$daynum = date("w", strtotime("wednesday"));
will return 0 for Sunday through to 6 for Saturday.
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).
What about using idate()? idate()
$integer = idate('w', $timestamp);
$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.
$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).
精彩评论