嗨,假设我的时间戳为"2005-10-16 13:05:41"。
我该如何创建一个变量,该变量在下一次从初始点到上午10点的时间是唯一的?
会是这样的吗?
$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);
我猜有什么绳子可以用来做这个?比如PHP文档中的"下周四"示例。
你差点…
$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);
编辑:这是基于您问题的标题,时间戳为第二天上午10点。如果你想在上午10点之前的任何时间在同一天上午10点输出,那么你需要添加一些额外的逻辑,就像那个白痴建议的那样。
第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
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