[英]PHP Date comparison with MySQL date field
I'm trying to compare the current date with a date field returned by a MySQL query called "expiry_date". 我正在尝试将当前日期与MySQL查询“ expiry_date”返回的日期字段进行比较。 This field is a date field formatted as
YYYY-MM-DD
I'm using If Else, to initialize a variable with the result as follows: 此字段是日期格式,格式为
YYYY-MM-DD
我使用If Else初始化变量,结果如下:
$today = $expiry_date = date("d-M-Y", strtotime("now"));
if ($row['expiry_date'] > $today)
{
$msg="Not Expired";
}
else
{
$msg="Expired";
}
This doesn't appear to work and I would appreciate any suggestions on how to fix this. 这似乎不起作用,我将对如何解决此问题提出任何建议。
$today = $expiry_date = new \Datetime();
$row_expiry_date = date_create_from_format('Y-m-d', $row['expiry_date']);
if ($row_expiry_date > $today)
{
$msg="Not Expired";
} else {
$msg="Expired";
}
You have two options, you can either format the dates the same and compare as strings 您有两个选择,您可以将日期设置为相同的格式并作为字符串进行比较
$today = $expiry_date = date("Y-m-d", strtotime("now"));
if ($row['expiry_date'] < $today)
or you can convert the row data to a unix timestamp for the comparison. 或者您可以将行数据转换为unix时间戳进行比较。
$today = $expiry_date = time();
if (strtotime($row['expiry_date']) < $today)
you have $today = $expiry_date =
which should be $today =
or $expiry_date =
您有
$today = $expiry_date =
应该是$today =
或$expiry_date =
$today = date("Y-m-d");
if (strtotime($row['expiry_date']) > $strtotime($today))
{
echo "not expired";
} else {
echo "expired";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.