Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 894
RonnyKnoxville Avatar asked Jan 26 '26 18:01

RonnyKnoxville


2 Answers

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

like image 102
Pitchinnate Avatar answered Jan 29 '26 08:01

Pitchinnate


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

Output: 6

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

like image 26
RonnyKnoxville Avatar answered Jan 29 '26 06:01

RonnyKnoxville