PHP get next occurrence of Monday from a certain date (with time)

前端 未结 1 813
南方客
南方客 2020-12-18 04:17

I\'m looking for the next Thursday after a specific date, say 2014-02-25. The problem I\'m having here is that when I use the below code, the time seems to be e

相关标签:
1条回答
  • 2020-12-18 04:52

    There is no time format that can directly express this. You need to produce a format like

    next Thursday 10:30:00
    

    ... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:

    $refdate = '2014-02-25 10:30:00';
    $timestamp = strtotime($refdate);
    
    echo date('Y-m-d H:i:s',
        strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
    );
    

    The same results could be achieved using string concatenation:

    echo date('Y-m-d', strtotime("next Thursday", $timestamp)
        . ' ' . date('H:i:s', $timestamp);
    

    The documentation for so called relative time formats can be found here

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