如何在PHP中获取具有特定时间的下一个日期 [英] How to get next Date with specific time in PHP
本文介绍了如何在PHP中获取具有特定时间的下一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在PHP中获取具有特定日期和时间的日期,就像我想要第二天的日期和时间是上午9.30,即 2011-06-02 09:30:00。
I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00". the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
上面的代码可以正常工作,但在31天后失败,在31日返回 1970-01-01 05:30:00。
还有其他方法吗?
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00". Is there any other way to do so.
推荐答案
将日期移动固定数字时,最好使用 mktime()
,因为它可以很好地处理无效日期(例如,它知道1月32日实际上是2月1日)
When shifting dates by a fixed number, it's better to use mktime()
, because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
这篇关于如何在PHP中获取具有特定时间的下一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文