php中的时间计算(加10小时)?
我得到时间:
$today = time();
$date = date('h:i:s A', strtotime($today));
如果当前时间是“上午 1:00:00”,我如何再添加 10 小时以变为上午 11:00:00?
I get the time:
$today = time();
$date = date('h:i:s A', strtotime($today));
if the current time is "1:00:00 am", how do i add 10 more hours to become 11:00:00 am??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
strtotime()
返回一个表示时间(以秒为单位)的数字。要增加它,请添加您要添加的相应秒数。 10 小时 = 60*60*10 = 36000,所以...编辑:我假设你在 $today 中有一个字符串时间 - 如果你只是使用当前时间,则更简单:
strtotime()
gives you a number back that represents a time in seconds. To increment it, add the corresponding number of seconds you want to add. 10 hours = 60*60*10 = 36000, so...Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:
请参阅 DateTime 类
(PHP 5 >= 5.2.0)
see DateTime Class
(PHP 5 >= 5.2.0)
您只需使用
DateTime
< /strong> 类,OOP 风格。You can simply make use of the
DateTime
class , OOP Style.$date = date('h:i:s A', strtotime($today . ' + 10 小时'));
(未经测试)
$date = date('h:i:s A', strtotime($today . ' + 10 hours'));
(untested)
现在显示的完整代码和添加的 10 分钟......
Full code that shows now and 10 minutes added.....
为了增加或减少时间,使用
strtotime
您可以在第一个参数中使用 相对格式 。在您的情况下,将当前时间增加 10 小时:
如果您需要将更改应用到另一个时间戳,则可以指定第二个参数。
注意:
因此,自 PHP 5.3 以来推荐的方式:
或使用别名:
在所有情况下,除非指定时区,否则将使用默认时区。
In order to increase or decrease time using
strtotime
you could use a Relative format in the first argument.In your case to increase the current time by 10 hours:
In case you need to apply the change to another timestamp, the second argument can be specified.
Note:
So, the recommended way since PHP 5.3:
or using aliases:
In all cases the default time zone will be used unless a time zone is specified.