php中的时间计算(加10个小时)?

myp*_*l00 22 php time addition

我有时间:

$today = time();
$date = date('h:i:s A', strtotime($today));
Run Code Online (Sandbox Code Playgroud)

如果当前时间是"凌晨1:00:00",我如何再增加10个小时才能成为上午11:00:00?

Amb*_*ber 50

strtotime()给你一个代表一个秒的时间的数字.要增加它,请添加要添加的相应秒数.10小时= 60*60*10 = 36000,所以......

$date = date('h:i:s A', strtotime($today)+36000); // $today is today date
Run Code Online (Sandbox Code Playgroud)

编辑:我原以为你今天有一个字符串时间 - 如果你只是使用当前时间,甚至更简单:

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

  • +1 是比在“strtotime”中添加“+ 10 小时”更优雅的解决方案:) (2认同)

小智 21

$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.
Run Code Online (Sandbox Code Playgroud)

请参阅DateTime类 (PHP 5> = 5.2.0)


jen*_*ram 6

$date = date('h:i:s A', strtotime($today . ' + 10 hours'));

(另)


Sha*_*ran 6

您可以简单地使用DateTimeOOP Style类.

<?php
$date = new DateTime('1:00:00');
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i:s a'); //"prints" 11:00:00 a.m
Run Code Online (Sandbox Code Playgroud)


Mar*_*arz 5

$date = date('h:i:s A', strtotime($today . " +10 hours"));
Run Code Online (Sandbox Code Playgroud)