37

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,834
  • 5
  • 26
  • 29

5 Answers5

97
$day_of_week = date('N', strtotime('Monday'));
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
22

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
  • 81,306
  • 22
  • 176
  • 206
16

What about using idate()? idate()

$integer = idate('w', $timestamp);
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 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,788
  • 8
  • 37
  • 58
3
$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
  • 33,871
  • 11
  • 91
  • 99