Search code examples
phpdatedatetimedayofweekweekday

Getting the Date and numeric weekday in PHP


i'm developing an application in PHP and I need to use dates and the numeric representation of weekdays.

I've tried the following:

$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', strtotime($tomorrow));
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";

Output

Today: 2016-11-11 weekday: 5
Tomorrow: 2016-11-12 weekday: 4

This isn't right because the weekday of tomorrow should be 6 instead of 4.

can someone help me out?


Solution

  • You have little error in the code, here`s the working one:

    $today = date("Y-m-d");
    $number = date('N', strtotime($today));
    echo "Today: " . $today . " weekday: " . $number . "<br>";
    
    $today = strtotime($today);
    $tomorrow = strtotime($today);
    $tomorrow = strtotime("+1 day", $today);
    $number2 = date('N', $tomorrow);
    echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";