PHP:日期大于当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5082261/
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
PHP: Date larger than current date
提问by Cameron
I have this code:
我有这个代码:
$curdate = '22-02-2011';
$mydate = '10-10-2011';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This would echo expired BUT shouldn't because $mydate is in the future and therefore smaller than the $curdate but PHP is looking at JUST the first two numbers 22 and 10 instead of the whole string. How can I fix this?
这会回显过期但不应该,因为 $mydate 是未来,因此小于 $curdate 但 PHP 只查看前两个数字 22 和 10 而不是整个字符串。我怎样才能解决这个问题?
Thanks
谢谢
回答by Zak
Try converting them both to timestamps first, and then compare two converted value:
尝试先将它们都转换为时间戳,然后比较两个转换后的值:
$curdate=strtotime('22-02-2011');
$mydate=strtotime('10-10-2011');
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This converts them to the number of seconds since January 1, 1970, so your comparison should work.
这会将它们转换为自 1970 年 1 月 1 日以来的秒数,因此您的比较应该有效。
回答by Chris Sobolewski
The problem is that your current variables are strings, and not time variables.
问题是您当前的变量是字符串,而不是时间变量。
Try this out:
试试这个:
$curdate = strtotime('22-02-2011');
$mydate = strtotime('10-10-2011');
回答by user7156400
$row_date = strtotime($the_date);
$today = strtotime(date('Y-m-d'));
if($row_date >= $today){
-----
}
回答by Chris Nash
Use the PHP date/time classes to convert these string representations into something you can directly compare using getTimestamp() to compare the UNIX times.
使用 PHP 日期/时间类将这些字符串表示形式转换为您可以使用 getTimestamp() 直接比较的内容,以比较 UNIX 时间。
If you're sure all your dates are in this format, you can string slice them into YYYY-MM-DD, and a string comparison will function correctly then.
如果您确定所有日期都采用这种格式,则可以将它们字符串切片为 YYYY-MM-DD,然后字符串比较将正常运行。
回答by Gaurav
if(strtotime($curdate) > strtotime($mydate))
{
...
}
回答by ER JUNAID LATEEF WANI
$currentDate = date('Y-m-d');
$currentDate = date('Y-m-d', strtotime($currentDate));
$startDate = date('Y-m-d', strtotime("01/09/2019"));
$endDate = date('Y-m-d', strtotime("01/10/2022"));
if (($currentDate >= $startDate) && ($currentDate <= $endDate)) {
echo "Current date is between two dates";
} else {
echo "Current date is not between two dates";
}
回答by Your Common Sense
it's VERY simple
这很简单
$curdate = '2011-02-22';
$mydate = '2011-10-10';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}