如何减去两个日期和时间来得到不同?
i have to sent an email when a user register email contain a link that is become invalid after six hours what i m doing when email is sent i update the db with field emailSentDate of type "datetime" now i got the curent date and time and has made to the same formate as it is in db now i want to find that both these dates and time have differenc of 6 hours or not so that i can make link invalid but i donot know how to do this
我必须发送电子邮件当用户注册后成为无效的电子邮件包含一个链接,六个小时我做什么当我发送电子邮件更新与“datetime类型的字段emailSentDate db的日期和时间,现在我得到了颇有相同的甲酸在db现在我想发现这些日期和时间都有differenc 6小时的,这样我可以让链接无效的但我不知道如何做到这一点
my code is look like this i m using hardcoded value for db just for example
我的代码是这样的,例如,我用硬编码的值为db
$current_date_time=date("Y-m-d h:i:s");
$current=explode(" ",$current_date_time);
$current_date=$current[0];
$current_time=$current[1];
$db_date_time="2010-07-30 13:11:50";
$db=explode(" ",$db_date_time);
$db_date=$db[0];
$db_time=$db[1];
i do not know how to proceed plz help
我不知道如何进行plz帮助
5 个解决方案
#1
19
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
打印2天,4小时,44分钟
see http://docs.php.net/datetime.diff
参见http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
编辑:但是您也可以将问题更多地转移到数据库端,例如,将过期日期/时间存储在表中,然后执行如下查询……当key='7gedufgweufg'和expires
更多相关文章
- 随手小记:PHP-FPM模式下PHP最大执行时间、Pragma和post-check
- PHP 导出 万级别数据 时间测试
- PHP结合Redis来限制用户或者IP某个时间段内访问的次数
- 使用Laravel在PHP中按时间戳,后端过滤SQL数据库查询
- 在PHP中如何取得两个日期时间相减的结果,得出大于一天,大于二天,大
- 如何检查时间偏移是否在夏令时?
- 当excel表格单元格的格式是日期格式非文本格式的时候,phpexcel 应
- 如何解决一段时间真正的循环,因为它昂贵且危险
- PHP格式化日期,显示“刚刚,几分钟前,几小时前,几天前,几个月前”