如何在 PHP 中比较两个日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3847736/
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
How can I compare two dates in PHP?
提问by Sarmen B.
How can I compare two dates in PHP?
如何在 PHP 中比较两个日期?
In the database, the date looks like 2011-10-2.
在数据库中,日期看起来像 2011-10-2。
If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?
如果我想将今天的日期与数据库中的日期进行比较以查看哪个更大,我该怎么做?
I tried this,
我试过这个,
$today = date("Y-m-d");
$expire = $row->expireDate //from db
if($today < $expireDate) { //do something; }
but it doesn't really work that way. What's another way of doing it?
但它并不是真的那样工作。另一种方法是什么?
Update: I know this post is kinda old but i just wanted to mention carbon, which is a class thats used with laravel but can be used with classic php and it does wonders with dates. check it out: Carbon
更新:我知道这篇文章有点旧,但我只想提一下 carbon,这是一个与 laravel 一起使用的类,但可以与经典的 php 一起使用,它对日期有奇效。看一看:碳
采纳答案by Matthew
in the database the date looks like this 2011-10-2
在数据库中,日期看起来像这样 2011-10-2
Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.
将其存储在 YYYY-MM-DD 中,然后字符串比较将起作用,因为 '1' > '0' 等。
回答by Hugo Peixoto
If all your dates are posterior to the 1st of January of 1970, you could use something like:
如果您的所有日期都晚于 1970 年 1 月 1 日,您可以使用以下内容:
$today = date("Y-m-d");
$expire = $row->expireDate; //from database
$today_time = strtotime($today);
$expire_time = strtotime($expire);
if ($expire_time < $today_time) { /* do Something */ }
If you are using PHP 5 >= 5.2.0, you could use the DateTime class:
如果您使用的是 PHP 5 >= 5.2.0,则可以使用 DateTime 类:
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($expire_dt < $today_dt) { /* Do something */ }
Or something along these lines.
或者类似的东西。
回答by d.raev
Just to compliment the already given answers, see the following example:
只是为了赞美已经给出的答案,请参阅以下示例:
$today = new DateTime('');
$expireDate = new DateTime($row->expireDate); //from database
if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) {
//do something;
}
Update:Or simple use old-school date()function:
更新:或者简单使用老式的date()函数:
if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
//echo not yet expired!
}
回答by Dr.Molle
I would'nt do this with PHP. A database should know, what day is today.( use MySQL->NOW() for example ), so it will be very easy to compare within the Query and return the result, without any problems depending on the used Date-Types
我不会用 PHP 做到这一点。数据库应该知道今天是几号。(例如使用 MySQL->NOW()),因此在查询中进行比较并返回结果将非常容易,根据使用的日期类型不会有任何问题
SELECT IF(expireDate < NOW(),TRUE,FALSE) as isExpired FROM tableName
回答by Atif Mahmood
$today = date('Y-m-d');//Y-m-d H:i:s
$expireDate = new DateTime($row->expireDate);// From db
$date1=date_create($today);
$date2=date_create($expireDate->format('Y-m-d'));
$diff=date_diff($date1,$date2);
//echo $timeDiff;
if($diff->days >= 30){
echo "Expired.";
}else{
echo "Not expired.";
}
回答by Shruthi reddy
$today_date=date("Y-m-d");
$entered_date=$_POST['date'];
$dateTimestamp1 = strtotime($today_date);
$dateTimestamp2 = strtotime($entered_date);
$diff= $dateTimestamp1-$dateTimestamp2;
//echo $diff;
if ($diff<=0)
{
echo "Enter a valid date";
}
回答by Syno
Here's a way on how to get the difference between two datesin minutes.
这是一种如何在几分钟内获得两个日期之间差异的方法。
// set dates
$date_compare1= date("d-m-Y h:i:s a", strtotime($date1));
// date now
$date_compare2= date("d-m-Y h:i:s a", strtotime($date2));
// calculate the difference
$difference = strtotime($date_compare1) - strtotime($date_compare2);
$difference_in_minutes = $difference / 60;
echo $difference_in_minutes;
回答by nwsmith
Here's my spin on how to get the difference in days between two dates with PHP. Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.
这是我关于如何使用 PHP 获得两个日期之间的天数差异的想法。注意'!'的使用 以丢弃日期的时间部分的格式,感谢来自DateTime createFromFormat without time 的信息。
$today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
$wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
$diff = $today->diff($wanted);
$days = $diff->days;
if (($diff->invert) != 0) $days = -1 * $days;
$overdue = (($days < 0) ? true : false);
print "<!-- (".(($days > 0) ? '+' : '').($days).") -->\n";
回答by kooli
Found the answer on a blog and it's as simple as:
在博客上找到了答案,很简单:
strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
And you'll get the days between the 2 dates.
你会得到两个日期之间的天数。
回答by isa
You can use simple PHP to compare dates:
您可以使用简单的 PHP 来比较日期:
$date = new simpleDate();
echo $date->now()->compare($expire_date)->isBeforeOrEqual();
This will give you true or false.
这会给你真假。
You can check the tutorials for more examples. Please click here.
您可以查看教程以获取更多示例。请点击这里。