0

if ステートメントを使用して 2 つの日付を比較し、別のクエリを実行して 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日以上に設定し、一部を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();

?>
4

1 に答える 1

0

私かもしれませんが、2つのオブジェクト(DateTime)を比較しているのですから、diff関数を使ってinvertフラグを取得した方が良いのではないでしょうか?

if ($boosterDate->diff($date30)->invert) {
    // other code    
} 
于 2013-11-01T08:24:59.170 に答える