PHP从某个日期(随着时间)获得星期一的下一次发生 [英] PHP get next occurrence of Monday from a certain date (with time)

查看:118
本文介绍了PHP从某个日期(随着时间)获得星期一的下一次发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在特定日期之后的下周四找到,说 2014-02-25 。我在这里遇到的问题是,当我使用下面的代码,时间似乎被删除。

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

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

我得到的结果是 2014-02-27 00:00: 00 当我想要 2014-02-27 10:30:00



我感谢您的帮助,谢谢。

解决方案

没有可以直接表达的时间格式。您需要生成一种格式,如

 下周四10:30:00 
/ pre>

...手动将其传递给 strtotime()。从参考时间字符串中提取的时间信息。像这样:

  $ refdate ='2014-02-25 10:30:00' 
$ timestamp = strtotime($ refdate);

echo date('Ymd H:i:s',
strtotime(next Thursday。date('H:i:s',$ timestamp),$ timestamp)
);

使用字符串连接可以实现相同的结果:


$ b $回覆日期('Ym-d',strtotime(next Thursday,$ timestamp)
。''。date('H:i:s' $ timestamp);

所谓的相对时间格式的文档可以找到 here


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.

解决方案

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

这篇关于PHP从某个日期(随着时间)获得星期一的下一次发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆