RonnyKnoxville
RonnyKnoxville

Reputation: 6394

Match the 'day of week' integer between PHP and MySQL

I cannot decide on the best method to match the 'day of week' number between MySQL and PHP

PHP

$date =  strtotime('2014-02-15');
$day_number = $day_of_week = date("w", $date);
$day = $day_of_week = date('l', $date);

echo $day." ".$day_number;

Output: Saturday 6

MySQL

SELECT DAYOFWEEK(  '2014-02-15' );

Output: 7

SELECT WEEKDAY(  '2014-02-15' );

Output: 5

Is there a way to match these numbers up without writing my own custom function?

Upvotes: 3

Views: 2192

Answers (2)

RonnyKnoxville
RonnyKnoxville

Reputation: 6394

SELECT DATE_FORMAT(  '2014-02-15',  '%w' )

Output: 6

(For reference, 2014-02-15 was a Saturday)

Upvotes: 4

Pitchinnate
Pitchinnate

Reputation: 7556

Well DAYOFWEEK has the following responses: (1 = Sunday, 2 = Monday, …, 7 = Saturday)

And in php date('w'); has the following responses: 0 (for Sunday) through 6 (for Saturday)

So you could just use date('w') + 1 and get the same answer in both.

http://www.php.net/manual/en/function.date.php

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek

Upvotes: 9

Related Questions