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

后端 未结 9 1808
说谎
说谎 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:39

    If your date is already a DateTime or DateTimeImmutable you can use the format method.

    $day_of_week = intval($date_time->format('w'));
    

    The format string is identical to the one used by the date function.


    To answer the intended question:

    $date_time->modify($target_day_of_week - $day_of_week . ' days');
    

提交回复
热议问题