我一直試圖用if語句比較兩個日期,然後運行另一個查詢並更新mysql數據庫,但未成功。我不知道我要去哪裏錯。如果我使用procedural
和使用試圖比較兩個日期...一個從MySQL DB存儲爲DATE和一個使用DateTime對象
date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date30)));
然後我得到列boosterWithinDays每一場更新。
如果我嘗試使用DateTime
對象
$boosterDate = new DateTime($row['boosterDate']);
$boosterDate->format("Y-m-d");
然後我沒有得到任何更新的。我已經設置了一些超過30天的BoosterDates,其中一些更少,沒有任何工作。
下面是完整的代碼:
<?php
//date_default_timezone_set('America/Chicago');
$todaysDate = new DateTime('now');
$formattedDate = $todaysDate->format('Y-m-d');
$date = new DateTime ('now');
$date90 = $date->add(new DateInterval('P90D'));
$date90 = $date->format('Y-m-d');
$date = new DateTime ('now');
$date60 = $date->add(new DateInterval('P60D'));
$date60 = $date->format('Y-m-d');
$date = new DateTime ('now');
$date30 = $date->add(new DateInterval('P30D'));
$date30 = $date->format('Y-m-d');
//echo $date90;
//echo "<br />";
//echo $formattedDate;
$sql = "SELECT * FROM service;";
if(!$result = $con->query($sql)){
die('There was an error running the query [' . $con->error . ']');
}
while($row = $result->fetch_array()){
//Get all the rows and store them in an array
$firstQueryRows[] = $row;
}
foreach($firstQueryRows as $row){
//do a new query with $row
$patientID = $row['patientID'];
$serviceID = $row['serviceID'];
//$boosterDate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $row['boosterDate'])));
//$date30 = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date30)));
//$date60 = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date60)));
//$date90 = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date90)));
$boosterDate = new DateTime($row['boosterDate']);
//$boosterDate->format("Y-m-d");
//$interval = $boosterDate->diff($date30);
$boosterDate = $boosterDate->format("Y-m-d");
var_dump($boosterDate > $date30);
echo "<br /><br />";
print "This is the booster date" . " " . ($boosterDate) . " ";
echo "<br /><br />";
print "This is todays date plus 30 days" . " " . ($date30) . " ";
if($boosterDate < $date30){
//echo "The date is less than 30" . " " . $row['serviceID'] . "<br /><br />";
//echo $date30 . "<br /><br />";
//echo $boosterDate;
echo "this is being shown because boosterDate object is less than date30 object";
$sql = "UPDATE service SET boosterWithinDays = 30;";
if(!$result = $con->query($sql)){
die('There was an error running the query [' . $con->error . ']');
}
}else{
NULL;
}
}
//$date = $row['boosterDate'];
//$id = $row['patientID'];
//echo $date . " " . $id . "<br /><br />";
//echo date_default_timezone_get();
?>
您對'電話 - >格式()'是不必要的 –
如果我走了 - >格式,它只是將所有的字段30,所以我還是堅持,爲什麼當我比較兩個日期不正確。 – ndjustin20