如何减去两个日期和时间来得到不同?

时间:2021-04-27 21:34:07

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 ()时,许多rdbms都对日期>

#2


3  

What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()

你所能做的就是将你的两个日期转换为Unix纪元时间,也就是,从1969年12月31日午夜起的等效秒数。由此,您可以很容易地推断出这两个日期之间经过的时间量。为此,您可以使用mktime()或strtotime()

All the best.

愿一切都好!

#3


2  

$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);

$hoursDiff = (time() - strtotime(“2010-07-30 13:11:50”)/(60 * 60);

#4


1  

I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.

我宁愿使用时间戳:将由“time()”返回的值作为“savedTime”保存到数据库(即以秒为单位的时间戳)。当你检查6个小时时,从“time()”中减去这个数字。

if ((time() - savedTime) > 6 * 3600) // more than 6h ago

如果(时间()- savedTime) > 6 * 3600) //超过6h。

or

"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)

“从savedTime <的表中选择”。(时间()- 6 * 3600)< p>

#5


0  

This might be the solution to your problem -> How to calculate the difference between two dates using PHP?

这可能是问题的解决方案——>如何使用PHP计算两个日期之间的差异?

#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 ()时,许多rdbms都对日期>

#2


3  

What you can do is convert both of your dates to Unix epoch times, that is, the equivalent number of seconds since midnight on the 31st of December 1969. From that you can easily deduce the amount of time elapsed between the two dates. To do this you can either use mktime() or strtotime()

你所能做的就是将你的两个日期转换为Unix纪元时间,也就是,从1969年12月31日午夜起的等效秒数。由此,您可以很容易地推断出这两个日期之间经过的时间量。为此,您可以使用mktime()或strtotime()

All the best.

愿一切都好!

#3


2  

$hoursDiff = ( time() - strtotime("2010-07-30 13:11:50") )/(60 * 60);

$hoursDiff = (time() - strtotime(“2010-07-30 13:11:50”)/(60 * 60);

#4


1  

I'd rather work with a timestamp: Save the value which is returned by "time()" as "savedTime" to your database (that's a timestamp in seconds). Subtract that number from "time()" when you check for your six hours.

我宁愿使用时间戳:将由“time()”返回的值作为“savedTime”保存到数据库(即以秒为单位的时间戳)。当你检查6个小时时,从“time()”中减去这个数字。

if ((time() - savedTime) > 6 * 3600) // more than 6h ago

如果(时间()- savedTime) > 6 * 3600) //超过6h。

or

"SELECT FROM table WHERE savedTime < " . (time() - 6 * 3600)

“从savedTime <的表中选择”。(时间()- 6 * 3600)< p>

#5


0  

This might be the solution to your problem -> How to calculate the difference between two dates using PHP?

这可能是问题的解决方案——>如何使用PHP计算两个日期之间的差异?