我需要在 PHP 中获取下一个 0900 日期。例如:
- 如果现在是0859,下一个0900就是今天0900
- 如果现在是0901,下一个0900就是明天0900
我只对 yyyymmdd 格式的日期感兴趣。关于如何做到这一点的任何建议?我在想这是我需要使用date
的strtotime
功能,但我只是不确定如何处理“下一个 0900”。
这不起作用:echo date('Ymd', strtotime('next 0900'));
这应该为您提供下一个日期9:00
:
date('Ymd', strtotime('9:00'));
编辑:不,实际上这不起作用。试试这个(不是那么简单,但有效):
// if the current time is greater than the time at 9:00, give 9:00 tomorrow
// Otherwise give 9:00 today
$nextnth = (mktime(9) < time()) ? strtotime('tomorrow 0900') : strtotime('today 0900');
echo date('Ymd', $nextnth);
尝试这个:
$x = strtotime('tomorrow 09:00') - strtotime("now");
if ($x > 86400) // If it's more than 1 day we're actually before 09:00, subtract 1 day
$x -= 86400;
echo "Now: ".date("Y-m-d H:i:s")."\r\n";
echo "Time until 0900: ".date("H\h i\m s\s", $x).".\r\n";
或者:
$x = (strtotime('tomorrow 09:00') - strtotime("now")) % 86400;
echo "Now: ".date("Y-m-d H:i:s")."\r\n";
echo "Time until 0900: ".date("H\h i\m s\s", $x).".\r\n";