How to find the date of a day of the week from a date using PHP ?

How to find the date of a day of the week from a date using PHP ?

Asked on December 28, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Here is the code, this works fine.

    $dayofweek = date('w', strtotime($date));
    $result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
    
    Answered on December 28, 2018.
    Add Comment

    We have to use the date() function through the following two ways.

    date('w'); // day of week
    

    or

    date('l'); // dayname
    

    Here is the example function for get the day nr,

    function getWeekday($date) {
        return date('w', strtotime($date));
    }
     
    echo getWeekday('2012-10-11'); // returns 4
    
    Answered on December 28, 2018.
    Add Comment

    One can use this code and find the date of a day of the week from a date.

    $date = '2012-10-11';
    $day = 1;
    $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday','Friday', 'Saturday');
    echo date('Y-m-d', strtotime($days[$day], strtotime($date)));
    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.