如何减去两个日期和时间来获得差异 [英] how to subtract two dates and times to get difference

查看:163
本文介绍了如何减去两个日期和时间来获得差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户注册电子邮件包含六个小时后无效的链接时,我必须发送电子邮件
发送电子邮件时正在做什么我用datetime类型的字段emailSentDate更新数据库
现在我得到了治愈日期和时间,并已经达到与db相同的格式,现在我想要发现,这两个日期和时间有6个小时不同,所以我可以使链接无效但我不知道如何做到这一点

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

我的代码看起来像这个im使用硬编码的值为db例如

my code is look like this i m using hardcoded value for db just for example

$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];

我不知道如何继续plz帮助

i do not know how to proceed plz help

推荐答案

<?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);

打印 2天,4小时44分钟

请参阅 http://docs.php。 net / datetime.diff

编辑:但是您也可以将问题更多地转移到数据库端,例如通过将过期日期/时间存储在表中,然后执行查询,如

... WHERE key ='7gedufgweufg'AND expires< Now()

许多rdbms对日期/时间算术有合理/良好的支持。

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.

这篇关于如何减去两个日期和时间来获得差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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