我一直在尝试使用 if 语句比较两个日期,然后运行另一个查询并更新 mysql 数据库,但没有成功。我不确定我哪里出错了。如果我使用procedural
和使用
date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date30)));
然后我得到了 boosterWithinDays 列中每个字段的更新。
如果我尝试使用DateTime
对象
$boosterDate = new DateTime($row['boosterDate']);
$boosterDate->format("Y-m-d");
然后我根本没有更新。我将一些 boosterDates 设置为超过 30 天,一些设置为更少,似乎没有任何效果。
这是完整的代码:
<?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();
?>