Trying to compare two dates…one from mysql DB stored as DATE
Posted: Thu Oct 31, 2013 10:54 pm
I have been trying unsuccessfully to compare two dates using an if statement to then run another query and update a mysql database. I'm not sure where I am going wrong. If I use procedural and use
date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date30)));
Then I get an update of every field in the column boosterWithinDays.
If I try and use DateTime object
$boosterDate = new DateTime($row['boosterDate']);
$boosterDate->format("Y-m-d");
Then I get no update at all. I've set some boosterDates to more than 30 days and some to less and nothing seems to work.
Here is the complete code:
date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date30)));
Then I get an update of every field in the column boosterWithinDays.
If I try and use DateTime object
$boosterDate = new DateTime($row['boosterDate']);
$boosterDate->format("Y-m-d");
Then I get no update at all. I've set some boosterDates to more than 30 days and some to less and nothing seems to work.
Here is the complete code:
Code: Select all
<?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");
if($boosterDate < $date30){
//echo "The date is less than 30" . " " . $row['serviceID'] . "<br /><br />";
//echo $date30 . "<br /><br />";
//echo $boosterDate;
$sql = "UPDATE service SET boosterWithinDays = 30;";
if(!$result = $con->query($sql)){
die('There was an error running the query [' . $con->error . ']');
}
}
}
//$date = $row['boosterDate'];
//$id = $row['patientID'];
//echo $date . " " . $id . "<br /><br />";
//echo date_default_timezone_get();
?>