0

陷入了一个复杂或愚蠢的问题。我从 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

4

1 回答 1

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'";

另请参阅此问题

于 2015-03-14T00:28:23.767 回答