user3442612 user3442612 - 1 year ago 65
PHP Question

Incorrect day number returned

for some reason I'm getting the correct day name, but incorrect day number when executing the following in PHP...

echo "date('l'): ".date('l'); // returns Thursday
echo "date('w'): ".date('w'); // returns 4

$dt = new DateTime();
var_dump($dt); // matches local time and date

public 'date' => string '2016-09-08 14:44:37' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Helsinki' (length=15)

echo $dt->format('w'); // returns 4

Strangely enough, I get 4 returned doing the same thing with JS in the browser calling getDay() so it doesn't appear to be language specific. Note: I'm using the LAMP stack.

Anyone know why this is? Thanks.

Answer Source

Reason is this

w gives Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)

w - gives 0 to 6 counting from sunday to saturday that's why you getting 4