以php计算时间(添加10小时)?

2022-08-30 15:48:08

我有时间:

$today = time();
$date = date('h:i:s A', strtotime($today));

如果当前时间是“1:00:00 am”,我如何再添加10个小时成为11:00:00 am?


答案 1

strtotime()给你一个数字,表示以秒为单位的时间。要递增它,请添加要添加的相应秒数。10 小时 = 60*60*10 = 36000,因此...

$date = date('h:i:s A', strtotime($today)+36000); // $today is today date

编辑:我假设你在$today中有一个字符串时间 - 如果你只是使用当前时间,那就更简单了:

$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already

答案 2
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.

请参阅日期时间类(PHP 5 > = 5.2.0)


推荐