php 将给定日期与今天进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113940/
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
Compare given date with today
提问by alexus
I have following
我有以下
$var = "2010-01-21 00:00:00.0"
I'd like to compare this date against today's date (i.e. I'd like to know if this $varis before today or equals today or not)
我想将这个日期与今天的日期进行比较(即我想知道这$var是在今天之前还是等于今天)
What function would I need to use?
我需要使用什么功能?
回答by Tyler Carter
strtotime($var);
Turns it into a time value
将其转换为时间值
time() - strtotime($var);
Gives you the seconds since $var
给你几秒钟以来 $var
if((time()-(60*60*24)) < strtotime($var))
Will check if $varhas been within the last day.
将检查是否$var在最后一天内。
回答by Bobby Hyman
That format is perfectly appropriate for a standard string comparison e.g.
该格式非常适合标准字符串比较,例如
if ($date1 > $date2){
//Action
}
To get today's date in that format, simply use: date("Y-m-d H:i:s").
为了让今天的这种格式的日期,只需使用:date("Y-m-d H:i:s")。
So:
所以:
$today = date("Y-m-d H:i:s");
$date = "2010-01-21 00:00:00";
if ($date < $today) {}
That's the beauty of that format: it orders nicely. Of course, that maybe less efficient, depending on your exact circumstances, but it might also be a whole lot more convenient and lead to more maintainable code - we'd need to know more to truly make that judgement call.
这就是这种格式的美妙之处:它的顺序很好。当然,这可能效率较低,具体取决于您的具体情况,但它也可能更方便并导致更易于维护的代码 - 我们需要了解更多才能真正做出判断。
For the correct timezone, you can use, for example,
对于正确的时区,您可以使用,例如,
date_default_timezone_set('America/New_York');
Click hereto refer to the available PHP Timezones.
单击此处参考可用的 PHP 时区。
回答by Alix Axel
Here you go:
干得好:
function isToday($time) // midnight second
{
return (strtotime($time) === strtotime('today'));
}
isToday('2010-01-22 00:00:00.0'); // true
Also, some more helper functions:
此外,还有一些辅助功能:
function isPast($time)
{
return (strtotime($time) < time());
}
function isFuture($time)
{
return (strtotime($time) > time());
}
回答by kamakazuu
To complete BoBby Hyman, the use of DateTime OBject, if you have php 5.2.2+ :
要完成 BoBby Hyman,使用 DateTime 对象,如果你有 php 5.2.2+ :
if(new DateTime() > new DateTime($var)){
// $var is before today so use it
}
回答by Salman A
You can use the DateTimeclass:
您可以使用DateTime该类:
$past = new DateTime("2010-01-01 00:00:00");
$now = new DateTime();
$future = new DateTime("2021-01-01 00:00:00");
Comparison operators work*:
比较运算符工作*:
var_dump($past < $now); // bool(true)
var_dump($future < $now); // bool(false)
var_dump($now == $past); // bool(false)
var_dump($now == new DateTime()); // bool(true)
var_dump($now == $future); // bool(false)
var_dump($past > $now); // bool(false)
var_dump($future > $now); // bool(true)
It is also possible to grab the timestamp values from DateTime objects and compare them:
也可以从 DateTime 对象中获取时间戳值并进行比较:
var_dump($past ->getTimestamp()); // int(1262286000)
var_dump($now ->getTimestamp()); // int(1431686228)
var_dump($future->getTimestamp()); // int(1577818800)
var_dump($past ->getTimestamp() < $now->getTimestamp()); // bool(true)
var_dump($future->getTimestamp() > $now->getTimestamp()); // bool(true)
* Note that ===returns false when comparing two different DateTime objects even when they represent the same date.
* 请注意,===即使比较两个不同的 DateTime 对象,即使它们表示相同的日期,也会返回 false。
回答by Hassan
$toBeComparedDate = '2014-08-12';
$today = (new DateTime())->format('Y-m-d'); //use format whatever you are using
$expiry = (new DateTime($toBeComparedDate))->format('Y-m-d');
var_dump(strtotime($today) > strtotime($expiry)); //false or true
回答by Nelu Bidonelu
Few years later, I second Bobby Hyman's observation that last 24 hrs is not today!!! And I am surprised that the answer was so much upvoted...
几年后,我支持 Bobby Hyman 的观察,即过去 24 小时不是今天!!!我很惊讶答案得到了如此多的支持......
To compare if a certain date is less, equal or greater than another, first you need to turn them "down" to beginning of the day. In other words, make sure that you're talking about same 00:00:00 time in both dates. This can be simply and elegantly done as:
要比较某个日期是否小于、等于或大于另一个日期,首先您需要将它们“降低”到一天的开始。换句话说,确保您在两个日期中谈论相同的 00:00:00 时间。这可以简单而优雅地完成:
strtotime("today") <=> strtotime($var)
if $varhas the time part on 00:00:00 like the OP specified.
如果$var在 00:00:00 有时间部分,就像指定的 OP 一样。
Replace <=>with whatever you need (or keep it like this in php 7)
替换<=>为您需要的任何内容(或在 php 7 中保持这样)
Also, obviously, we're talking about same timezone for both. For list of supported TimeZones
此外,显然,我们正在谈论两者的时区相同。对于支持时区列表
回答by Lynnell Emmanuel Neri
One caution based on my experience, if your purpose only involves date then be careful to include the timestamp. For example, say today is "2016-11-09". Comparison involving timestamp will nullify the logic here. Example,
基于我的经验的一个警告,如果您的目的仅涉及日期,那么请小心包含时间戳。例如,说今天是"2016-11-09"。涉及时间戳的比较将使这里的逻辑无效。例子,
// input
$var = "2016-11-09 00:00:00.0";
// check if date is today or in the future
if ( time() <= strtotime($var) )
{
// This seems right, but if it's ONLY date you are after
// then the code might treat $var as past depending on
// the time.
}
The code above seems right, but if it's ONLY the date you want to compare, then, the above code is not the right logic.Why? Because, time() and strtotime() will provide include timestamp. That is, even though both dates fall on the same day, but difference in time will matter. Consider the example below:
上面的代码看起来是对的,但是如果只是你想要比较的日期,那么上面的代码就不是正确的逻辑。为什么?因为, time() 和 strtotime() 将提供包含时间戳。也就是说,即使两个日期都在同一天,但时间差异很重要。考虑下面的例子:
// plain date string
$input = "2016-11-09";
Because the input is plain date string, using strtotime()on $inputwill assume that it's the midnight of 2016-11-09. So, running time()anytime after midnight will always treat $inputas past, even though they are on the same day.
因为输入是纯日期字符串,所以使用strtotime()on$input将假定它是 2016-11-09 的午夜。因此,即使在同一天,time()在午夜之后的任何时候运行都将始终$input视为过去。
To fix this, you can simply code, like this:
要解决此问题,您可以简单地编写代码,如下所示:
if (date("Y-m-d") <= $input)
{
echo "Input date is equal to or greater than today.";
}
回答by Richard de Ree
$date1=date_create("2014-07-02");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
(the w3schools example, it works perfect)
(w3schools 示例,它完美无缺)
回答by enceladus
Expanding on Josua's answer from w3schools:
扩展来自 w3schools 的 Josua 的回答:
//create objects for the dates to compare
$date1=date_create($someDate);
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date1,$date2);
//now convert the $diff object to type integer
$intDiff = $diff->format("%R%a");
$intDiff = intval($intDiff);
//now compare the two dates
if ($intDiff > 0) {echo '$date1 is in the past';}
else {echo 'date1 is today or in the future';}
I hope this helps. My first post on stackoverflow!
我希望这有帮助。我在 stackoverflow 上的第一篇文章!

![$_SERVER['DOCUMENT_ROOT'] 在通过 cron 运行的 php 脚本中不起作用](/res/img/loading.gif)