Home » Php » php – day of the week to day number (Monday = 1, Tuesday = 2)

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

$daynum = func('wednesday'); //echos 3
How to&Answers:
$day_of_week = date('N', strtotime('Monday'));

Answer:

What about using idate()? idate()

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

Answer:

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).

Answer:

$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.

Answer:

$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).