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

发布于 2024-08-09 23:36:27 字数 156 浏览 2 评论 0原文

我得到时间:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

菊凝晚露 2024-08-16 23:36:27

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

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

$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:

$date = date('h:i:s A', time()+36000); // time() returns a time in seconds already
待天淡蓝洁白时 2024-08-16 23:36:27
$tz = new DateTimeZone('Europe/London');
$date = new DateTime($today, $tz);
$date->modify('+10 hours');
// use $date->format() to outputs the result.

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

$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)

十六岁半 2024-08-16 23:36:27

您只需使用 DateTime< /strong> 类,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

You can simply make use of the DateTime class , OOP 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
偷得浮生 2024-08-16 23:36:27

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

(未经测试)

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

(untested)

画▽骨i 2024-08-16 23:36:27
$date = date('h:i:s A', strtotime($today . " +10 hours"));
$date = date('h:i:s A', strtotime($today . " +10 hours"));
回眸一遍 2024-08-16 23:36:27

现在显示的完整代码和添加的 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;

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

$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;
空宴 2024-08-16 23:36:27

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

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

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

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

注意:

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

因此,自 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');

或使用别名:

$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 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:

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

So, the recommended way since 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文