如何使用php在变量中添加特定小时的时间?

时间:2017-04-16 11:18:35

标签: php time

如何用php中的不同小时显示时间。例如,我有2个变量:

$start_hour = "08:00"; 
$different_hour = "1";

我希望输出如下:

  

08:00

     

09:00

实际上我希望它像this一样。但我怎么能在PHP中实现它。谢谢。

1 个答案:

答案 0 :(得分:0)

您需要使用strtotime()将字符串转换为时间戳,并将时间戳添加一小时。在函数参数的字符串时间开始写+1hour以添加一小时的时间。最后,使用date()

将时间戳转换为时间
$start_hour = "08:00";
$different_hour = 1;
$newTime = date("H:i", strtotime("+{$different_hour}hour ".$start_hour));
// or
$newTime = date("H:i", strtotime($start_hour) + ($different_hour*60*60));

demo

中检查代码结果