在PHP中添加时间 [英] Adding time in PHP

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

问题描述

我从mysql数据库中提取datetime,我想添加X小时,然后将其与当前时间进行比较。到目前为止我有

I am pulling a datetime from a mysql db and i would like to add X hours to it then compare it to the current time. So far i got

$dateNow = strtotime(date('Y-m-d H:i:s'));
$dbTime = strtotime($row[0]);

然后我尝试$ dbTime + strtotime(4小时);但4小时似乎添加4小时的当前时间,而不是原始的4小时。如何添加X小时到dbTime?

then i tried $dbTime + strtotime("4 hours"); but 4 hours seem to add 4hrs to the current time instead of raw 4hours. How do i add X hours to dbTime?

注意:我使用的是php 5.1.2所以date_add不工作(5.3.0)

NOTE: I am using php 5.1.2 so date_add doesnt work (5.3.0)

time()和strtotime()结果以秒为单位的unix时间戳,所以你可以做类似下面的事情,提供你的数据库并做比较:

解决方案

p>

time() and strtotime() result in unix timestamps in seconds, so you can do something like the following, provided your db and do your comparison:

$fourHours = 60 * 60 * 4;
$futureTime = time() + $fourHours;

这篇关于在PHP中添加时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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