php 向时间变量添加 1 小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8552106/
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-26 04:58:40  来源:igfitidea点击:

Adding 1 hour to time variable

php

提问by paulcol.

This simple thing has wasted hours of my time trying to get it working, still havnt worked it out...

这个简单的事情浪费了我数小时的时间试图让它工作,但仍然没有解决......

i have

我有

$time = '10:09';

I want to add an hour to that...

我想增加一个小时...

So, I've tried:

所以,我试过:

$time = strtotime('+1 hour');

strtotime('+1 hour', $time);

$time = date('H:i', strtotime('+1 hour'));

None of the above worked.

以上都没有奏效。

Can you guys help me out?

你们能帮我吗?

Thanks.

谢谢。

回答by paulcol.

Worked for me..

为我工作..

$timestamp = strtotime('10:09') + 60*60;

$time = date('H:i', $timestamp);

echo $time;//11:09


Explanation:

解释:

strtotime('10:09')creates a numerical timestamp in seconds, something like 1510450372. Simply add or remove the amount of seconds you need and use date()to convert it back into a human readable format.

strtotime('10:09')创建一个以秒为单位的数字时间戳,类似于1510450372. 只需添加或删除您需要的秒数date(),并将其转换回人类可读的格式。

$timestamp = strtotime('10:09') + 60*60; // 10:09 + 1 hour
$timestamp = strtotime('10:09') + 60*60*2; // 10:09 + 2 hours
$timestamp = strtotime('10:09') - 60*60; // 10:09 - 1 hour

time()also creates a numerical timestamp but for right now. You can use it in the same way.

time()还创建一个数字时间戳,但现在。您可以以相同的方式使用它。

$timestamp = time() + 60*60; // now + 1 hour

回答by Azam Alvi

You can do like this

你可以这样做

    echo date('Y-m-d H:i:s', strtotime('4 minute'));
    echo date('Y-m-d H:i:s', strtotime('6 hour'));
    echo date('Y-m-d H:i:s', strtotime('2 day'));

回答by Frosty Z

$time = '10:09';
$timestamp = strtotime($time);
$timestamp_one_hour_later = $timestamp + 3600; // 3600 sec. = 1 hour

// Formats the timestamp to HH:MM => outputs 11:09.
echo strftime('%H:%M', $timestamp_one_hour_later);
// As crolpa suggested, you can also do
// echo date('H:i', $timestamp_one_hour_later);

Check PHP manual for strtotime(), strftime()and date()for details.

有关详细信息,请查看 PHP 手册中的strtotime()strftime()date()

BTW, in your initial code, you need to add some quotes otherwise you will get PHP syntax errors:

顺便说一句,在您的初始代码中,您需要添加一些引号,否则您将收到 PHP 语法错误:

$time = 10:09; // wrong syntax
$time = '10:09'; // syntax OK

$time = date(H:i, strtotime('+1 hour')); // wrong syntax
$time = date('H:i', strtotime('+1 hour')); // syntax OK

回答by Mayank Vadiya

try this it is worked for me.

试试这个它对我有用。

$time="10:09";
$time = date('H:i', strtotime($time.'+1 hour'));
echo $time;

回答by Akbor

for this problem please follow bellow code:

对于此问题,请遵循以下代码:

$time= '10:09';
$new_time=date('H:i',strtotime($time.'+ 1 hour'));
echo $new_time;`
// now output will be: 11:09

回答by Learner

2020 Update

2020 更新

It is weird that no one has suggested the OOP way:

奇怪的是,没有人提出 OOP 方式:

$date = new \DateTime(); //now
$date->add(new \DateInterval('PT3600S'));//add 3600s / 1 hour

OR

或者

$date = new \DateTime(); //now
$date->add(new \DateInterval('PT60M'));//add 60 min / 1 hour

OR

或者

$date = new \DateTime(); //now
$date->add(new \DateInterval('PT1H'));//add 1 hour

Extract it in string with format:

将其提取为字符串格式:

var_dump($date->format('Y-m-d H:i:s'));

I hope it helps

我希望它有帮助

回答by Ladislav Něme?ek

Simple and smart solution:

简单而智能的解决方案:

date("H:i:s", time()+3600);

回答by Ricardo Souza

You can use:

您可以使用:

$time = strtotime("10:09") + 3600;
echo date('H:i', $time);

Or date_add: http://www.php.net/manual/en/datetime.add.php

或者date_addhttp: //www.php.net/manual/en/datetime.add.php

回答by user3298040

Beware of adding 3600!! may be a problem on day change because of unix timestamp format uses moth before day.

小心加3600!!由于 unix 时间戳格式在一天之前使用蛾,因此可能会在日期更改时出现问题。

e.g. 2012-03-02 23:33:33 would become 2014-01-13 13:00:00 by adding 3600 better use mktime and date functions they can handle this and things like adding 25 hours etc.

例如 2012-03-02 23:33:33 将变成 2014-01-13 13:00:00 通过添加 3600 更好地使用 mktime 和 date 函数,他们可以处理这个问题以及添加 25 小时等。

回答by baladeva juganas

You can try this code:

你可以试试这个代码:

$time = '10:09';

echo date( 'H:i', strtotime( '+1 hour' , strtotime($time) ) );