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

后端 未结 9 1715
说谎
说谎 2020-11-27 03:59

If I\'ve got a $date YYYY-mm-dd and want to get a specific $day (specified by 0 (sunday) to 6 (saturday)) of the week that YYYY-

相关标签:
9条回答
  • 2020-11-27 04:41

    You can use the date() function:

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

    or

    date('l'); // dayname
    

    Example function to get the day nr.:

    function getWeekday($date) {
        return date('w', strtotime($date));
    }
    
    echo getWeekday('2012-10-11'); // returns 4
    
    0 讨论(0)
  • 2020-11-27 04:42

    I'm afraid you have to do it manually. Get the date's current day of week, calculate the offset and add the offset to the date.

    $current = date("w", $date)
    $offset = $day - $current
    $new_date = new DateTime($date)
        ->add(
            new DateInterval($offset."D")
        )->format('Y-m-d')
    
    0 讨论(0)
  • 2020-11-27 04:48
    <?php echo date("H:i", time()); ?>
    <?php echo $days[date("l", time())] . date(", d.m.Y", time()); ?>
    

    Simple, this should do the trick

    0 讨论(0)
提交回复
热议问题