给时间变量增加1小时 [英] Adding 1 hour to time variable

查看:77
本文介绍了给时间变量增加1小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的东西浪费了我数小时的时间来尝试使其工作,但仍然无法解决...

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

我有

$time = '10:09';

我想增加一个小时...

I want to add an hour to that...

所以,我试过了:

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

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

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

以上方法均无效.

你们能帮我吗?

谢谢.

推荐答案

为我工作.

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

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

echo $time;//11:09


说明:


Explanation:

strtotime('10:09')创建以秒为单位的数字时间戳,类似于1510450372.只需添加或删除所需的秒数,然后使用date()将其转换回人类可读的格式即可.

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.

$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()也会创建一个数字时间戳,但目前为止.您可以以相同的方式使用它.

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

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

这篇关于给时间变量增加1小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆