两个日期之间的PHP日期比较

时间:2014-12-15 03:45:51

标签: php mysql date

如果在2天内没有产品发货,我想跟踪状态,然后必须有通知,通过电子邮件提醒他们发货。发货日期可以从电子邮件日期获取,但如果我进行比较则可以:

//MAX(date_shipment = latest date record of shipment in database

if (MAX(date_shipment) - dateNow() >= 2 days )
{
   $update ="update table1 set remarks = 'No shipment received since 2 days ago', status = 'warning'";
   $result = mysql_query($update);
}
else
{ }

但我认为存在问题,因为此代码与dateNow()进行比较。如果收到新货物,仍然会运行日期比较代码,但不会与收到的新发货日期进行比较。我不知道如何解释以及如何开始这项功能。

有人可以建议解决这个问题的正确方法是什么?提前致谢

2 个答案:

答案 0 :(得分:0)

让我们试试这段代码:

$currentDate = getdate();
$anotherDate = MAX(date_shipment); //whatever this is

//this will give sql format to the current date
$year = $currentDate['year'];
$month = $currentDate['mon'];
$day = $currentDate['mday'];
$currentDate = $year.'-'.$month.'-'.$day;

$date1 = date_create($anotherDate); //assuming your date has sql format
$date2 = date_create($currentDate);

$difference = date_diff($date1, $date2);
$difference = str_replace(" days","",$difference->format('%R%a days'));

if ($difference >= 2){
    $update ="update table1 set remarks = 'No shipment received since 2 days ago', status = 'warning'";
    $result = mysql_query($update);
}
else
{ } //I don't get what you want here

答案 1 :(得分:0)

这似乎就像你想要做的那样。不要再使用mysql了,请使用mysqli。

$dbDate = mysql_query("SELECT `date_shipment` FROM table1");

$addedDate = new DateTime();
$addedDate->modify('+2 day');

if ($dbDate['date_shipment'] >= $addedDate)
{
   $update ="UPDATE table1 SET remarks = 'No shipment received since 2 days ago',
   status = 'warning'";
   $result = mysql_query($update);
}
else
{ }