1

嗨,假设我的时间戳为“2005-10-16 13:05:41”。

我将如何创建一个变量,该变量将具有下一次从该初始点变为上午 10 点的 unixtime?

会是这样吗?

$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);

我猜我可以用一些字符串来做到这一点?就像 PHP 文档中的“下周四”示例一样。

4

2 回答 2

5

你差点吃...

$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);

编辑:这是基于您问题的标题,时间戳为第二天上午 10 点。如果您想在同一天上午 10 点之前的任何时间输出上午 10 点,那么您需要添加一些额外的逻辑,正如 thatidiotguy 建议的那样。

编辑2:

出于某种原因,如果将所有逻辑放在同一个 strtotime 方法中,它将无法工作,所以我做了一个简单的函数。您可以轻松地将其放在一行中,但我将其保留为 2 以使其更清晰:

$time1 = strtotime('-2 days 09:59:59');
$time2 = strtotime('-2 days 10:00:01');

function next_10am($time)
{
    $temp = strtotime('+1 day -10 hours', $time);
    return strtotime('10:00', $temp);
}

echo next_10am($time1); // Outputs: 2012-09-08 10:00:00
echo next_10am($time2); // Outputs: 2012-09-09 10:00:00
于 2012-09-10T22:10:19.870 回答
2

没有办法strtotime知道上午 10 点是否已经过去,所以我会这样做:

$timestamp = strtotime("2005-10-16 13:05:41");
// Get current hour and if it is > 10 add a day
if (date('G',$timestamp) >= 10) {
    $tenAMTime = strtotime("+1 day 10am", $timestamp);
}
else {
    $tenAMTime = strtotime("10am", $timestamp);
}
echo date('r',$tenAMTime); // Comment this out if you want
于 2012-09-10T22:13:45.887 回答