タイトルが示すように、PHP は単純な if 比較ステートメントで、返すべきものとは逆の結果を返すことに本当に混乱しています。最初に文字列に変換された2つの日時を比較しようとしています:
//Fetched db query, this returns 2012-06-23 16:00:00
$databaseDateTime = strtotime($row['time']);
//This now returns 1340481600
//today's date and time I'm comparing to, this returns 2012-06-22 17:14:46
$todaysDateTime = strtotime(date("Y-m-d H:i:s"));
//this now returns 1340399686
これまでのところ、すべてが完璧に機能しています。さて、ここで物事が毛むくじゃらになります:
if ($databaseDateTime < $todaysDateTime) { $eventType = 'past'; }
そして、これは「過去」を返しますが、もちろんそうすべきではありません。何か足りないと教えてください。私のプロジェクトの種類は、この機能が密閉されていることに依存しています。
**編集*** 私を助けてくれてありがとう。より多くのコンテキストが必要な人もいるので、コード全体を投稿させてください。リクエストは IOS5 からバックエンド コードに送られ、json が電話に送り返されています。
<?php
//all included files including $link to mysqli_db and function sendResponse()
function getEvents($eventType, $eventArray) {
global $link;
global $result;
global $i;
global $todaysDateTime;
foreach ($eventArray as $key => $value) {
$sqlGetDeal = mysqli_query($link, "SELECT time FROM deals WHERE id='$value' AND active='y' LIMIT 1") or die ("Sorry there has been an error!");
while ($row = mysqli_fetch_array($sqlGetDeal)) {
//compare times to check if event already happened
$databaseDateTime = strtotime($row['time']);
if ($databaseDateTime < $todaysDateTime) { $eventType = 'past'; }
$result[$i] = array(
'whenDeal' => $eventType,
'time' => $databaseDateTime,
);
$i++;
}//end while
}//end foreach
}
if (isset($_GET['my'])) {
//$_GET['my'] comes in as a string of numbers separated by commas e.g. 3,2,6,3
$myDeals = preg_replace('#[^0-9,]#', '', $_GET['my']);
$todaysDateTime = strtotime(date("Y-m-d H:i:s"));
$result = array();
$kaboomMy = explode(",", $myDeals);
$i = 1;
if ($myEvents != "") {
getEvents('future', $kaboomMy);
}//end if
sendResponse(200, json_encode($result));
} else {
sendResponse(400, 'Invalid request');
} //end $_POST isset
?>