未能将php日期与mysql日期进行比较

时间:2012-12-13 22:25:03

标签: php mysql date comparison string-comparison

陷入了一个复杂或可能是愚蠢的问题。我从mysql得到一个查询,然后尝试比较一个日期列与PHP数据,我格式化为相同的格式,即“Ymd”它总是导致没有匹配,虽然我看到有匹配..它得到正确的结果也是如此。

<?php

date_default_timezone_set('America/Los_Angeles'); // set timezone to our timezone
$constantTime = time(); // get value of time in constant
$appDate = date("Y-m-d", $constantTime); //that defines php time variable - 
$queryDate = "SELECT * FROM date WHERE date='$appDate'";
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resulDate);
if ($appDate == date("Y-m-d", strtotime($recordDate['date']))) {
    echo "MATCH    ";
    $dateID = $recordDate['dateID'];
} else {
    mysql_query("insert into date(date) values('$appDate')")or die("Database write error1");
    $resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
    $recordDate = mysql_fetch_array($resultDate);
    echo "NO MATCH ";
    $dateID = $recordDate['dateID'];
}

这总是触发else,我试过===而不是==,我试过strcmp

1 个答案:

答案 0 :(得分:0)

我假设您正在比较日期时间字段,您有两种可能性:

迄今为止的投放字段:

$queryDate = "SELECT * FROM your_table WHERE date(your_date_field) = date('$appDate')";

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date

修改您的日期格式以与ISO兼容:

$appDate = date("Y-m-d H:i:s", $constantTime); //it defines date in format 2015-03-14 15:00:00
$queryDate = "SELECT * FROM your_table WHERE your_date_field='$appDate'";

另见this question