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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1665702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:26:27  来源:igfitidea点击:

Time calculation in php (add 10 hours)?

phptimeaddition

提问by myphpsql00

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??

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

回答by Amber

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...

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

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

Edit: I had assumed you had a string time in $today - if you're just using the current time, even simpler:

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

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

回答by myphpsql00

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

see DateTime Class(PHP 5 >= 5.2.0)

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

回答by Shankar Damodaran

You can simply make use of the DateTimeclass , OOP Style.

您可以简单地使用DateTime类,OOP 样式。

<?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

回答by jensgram

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

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

(untested)

(未经测试)

回答by Marek Karbarz

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

回答by Mikeys4u

Full code that shows now and 10 minutes added.....

现在显示的完整代码和 10 分钟添加.....

$nowtime = date("Y-m-d H:i:s");
echo $nowtime;
$date = date('Y-m-d H:i:s', strtotime($nowtime . ' + 10 minute'));
echo "<br>".$date;

回答by Nik

In order to increase or decrease time using strtotimeyou could use a Relative formatin the first argument.

为了增加或减少使用时间,strtotime您可以在第一个参数中使用相对格式

In your case to increase the current timeby 10 hours:

在您的情况下,将当前时间增加 10 小时:

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

In case you need to apply the change to another timestamp, the second argument can be specified.

如果您需要将更改应用于另一个时间戳,则可以指定第二个参数。

Note:

笔记:

Using this function for mathematical operations is not advisable. It is better to use DateTime::add()and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

不建议将此函数用于数学运算。DateTime::add()在 PHP 5.3 及更高版本中最好使用和 DateTime::sub(),或者在 PHP 5.2 中使用 DateTime::modify()。

So, the recommended waysince PHP 5.3:

所以,自 PHP 5.3 起推荐的方式

$dt = new DateTime(); // assuming we need to add to the current time
$dt->add(new DateInterval('PT10H'));
$date = $dt->format('h:i:s A');

or using aliases:

或使用别名:

$dt = date_create(); // assuming we need to add to the current time
date_add($dt, date_interval_create_from_date_string('10 hours')); 
$date = date_format($dt, 'h:i:s A');

In all cases the default time zone will be used unless a time zone is specified.

在所有情况下,除非指定时区,否则将使用默认时区。