php 如何减去两个日期和时间以获得差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3371011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:29:46  来源:igfitidea点击:

how to subtract two dates and times to get difference

phpdatetime

提问by user403269

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 中相同的格式,现在我想发现这些日期和时间是否有 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

我不知道如何进行请帮助

回答by VolkerK

<?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 days, 4 hours, 44 minutes

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.

编辑:但您也可以将问题更多地转移到数据库端,例如通过将到期日期/时间存储在表中,然后进行查询,例如
... WHERE key='7gedufgweufg' AND expires<Now()
许多 rdbms 对日期/时间算术有合理/良好的支持。

回答by Adam

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.

祝一切顺利。

回答by ajile

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

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

回答by Select0r

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”保存到您的数据库中(这是一个以秒为单位的时间戳)。当您检查六个小时时,从“time()”中减去该数字。

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

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

or

或者

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

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

回答by Aditya

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

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