[英]Comparing Date from PHP to MYSQL, strtotime not working
I have a DATE field on my MySQL table. 我的MySQL表上有一个DATE字段。 Let's say the value is 2015-05-05.
假设值为2015-05-05。 I would like to check if the current time is before or after that.
我想检查当前时间是在此之前还是之后。 This is my code:
这是我的代码:
foreach ($row as $row)
{
$ExpDate = strtotime($row['exp_date']);
$Today = strtotime(date("Y-m-d"));
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
The problem is that it's not working. 问题是它不起作用。 The value of exp without strtotime is 2015-05-05.
不带strtotime的exp的值为2015-05-05。 It I add the strtotime, the value becomes an empty string.
我添加了strtotime,该值变成了一个空字符串。
How am I able to solve this problem or what would be a good way to compare dates in PHP? 我如何解决这个问题,或者用什么方法比较PHP中的日期?
When comparing dates in MySQL there is no reason to take the extra step and use string_to_time()
. 在MySQL中比较日期时,没有理由采取额外的步骤并使用
string_to_time()
。 The below example should work just fine. 下面的示例应该可以正常工作。 The format of MySQL DATE is designed in such a way that comparisons of this nature work naturally without any extra steps needed.
MySQL DATE的格式经过设计,可以自然地进行这种性质的比较,而无需任何额外的步骤。
foreach ($row as $row)
{
$ExpDate = $row['exp_date'];
$Today = date("Y-m-d");
echo $Today . ' - ' . $ExpDate;
if ($Today > $ExpDate)
{
exit('<M>LicenseExpired<M>');
}
}
try this 尝试这个
foreach ($row as $row)
{
$ExpDate = new DateTime($row['exp_date']);
$Today = new DateTime(date("Y-m-d"));
$interval = $ExpDate->diff($Today);
//echo $interval->format('%R%a days'); <--- to diffenernce by days.
if ($interval > 0)
{
exit('<M>LicenseExpired<M>');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.