36

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
zmol
  • 2,734
  • 5
  • 25
  • 29

5 Answers5

92
$day_of_week = date('N', strtotime('Monday'));
ceejayoz
  • 171,474
  • 40
  • 284
  • 355
20

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

Leon M
  • 185
  • 1
  • 11
adrianbanks
  • 79,167
  • 22
  • 173
  • 203
15

What about using idate()? idate()

$integer = idate('w', $timestamp);
Damien Pirsy
  • 25,003
  • 8
  • 68
  • 77
12
$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.

Wige
  • 3,627
  • 8
  • 33
  • 54
2
$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).

Linus Kleen
  • 32,634
  • 11
  • 88
  • 99