我现在有PHP获取当前时间/日期,像这样:增加“X”的时间量至今
$now = date("Y-m-d H:m:s");
喜欢什么ID做的是有一个新的变量$new_time
等于$now
+ $hours
。 $hours
是的时间从任何地方24-800范围的量。
有什么建议吗?
我现在有PHP获取当前时间/日期,像这样:增加“X”的时间量至今
$now = date("Y-m-d H:m:s");
喜欢什么ID做的是有一个新的变量$new_time
等于$now
+ $hours
。 $hours
是的时间从任何地方24-800范围的量。
有什么建议吗?
您可以使用类似strtotime()
功能的东西添加到当前时间戳。 $new_time = date("Y-m-d H:i:s", strtotime('+5 hours'))
。
如果您需要函数中的变量,则必须使用双引号,然后像strtotime("+{$hours} hours")
那样,但是您可以更好地使用strtotime(sprintf("+%d hours", $hours))
。
您可以使用strtotime()来实现这一目标:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
您还可以使用UNIX风格的时间来计算:
$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";
嗯......你分钟应予纠正......“我'是分钟。不是几个月。 :)(我有同样的问题的东西太多
$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
的其他解决方案(面向对象)是使用日期时间::加
例:
$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT{$hours}H"));
$new_time = $now->format('Y-m-d H:i:s');
这里的小tyop,$ date->格式可能应该是$ now-> format – Patrick 2015-01-11 22:18:48
我使用以下函数将正常的日期时间值转换为mysql日期时间格式:
private function ampmtosql($ampmdate) {
if($ampmdate == '')
return '';
$ampm = substr(trim(($ampmdate)), -2);
$datetimesql = substr(trim(($ampmdate)), 0, -3);
if ($ampm == 'pm') {
$hours = substr(trim($datetimesql), -5, 2);
if($hours != '12')
$datetimesql = date('Y-m-d H:i',strtotime('+12 hour',strtotime($datetimesql)));
}
elseif ($ampm == 'am') {
$hours = substr(trim($datetimesql), -5, 2);
if($hours == '12')
$datetimesql = date('Y-m-d H:i',strtotime('-12 hour',strtotime($datetimesql)));
}
return $datetimesql;
}
它可以转换像datetime值,
2015-06-04 09:55 AM -> 2015-06-04 09:55
2015-06-04 03:55 PM -> 2015-06-04 15:55
2015-06-04 12:30 AM -> 2015-06-04 00:55
希望这会帮助别人。
你可以试试LIB Ouzo goodies,并用流利的方式做到这一点:
echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");
的API允许多个操作。
正确
可以使用的strtotime()来实现这一目标:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours
的strtotime()http://php.net/manual/en/function.strtotime.php – 2012-07-08 20:14:24
我怀疑如果这是你想要的。我希望你注意到“H:m:s”中的“m”代表数字上的月份。我相信你想写“H:我:S”。请交叉检查 – Paullo 2014-08-14 14:59:57