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

核能气质少年 提交于 2019-11-28 01:00:46

问题


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 erased.

<?php
    $timestamp = '2014-02-25 10:30:00';

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

The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00

Is there something I am missing here that is causing the time to be set to midnight?

I appreciate the help, thanks.


回答1:


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



来源:https://stackoverflow.com/questions/22055935/php-get-next-occurrence-of-monday-from-a-certain-date-with-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!