Reputation: 224
I am trying to compare a user inputed date and time with a date and time stored in a MySQL database (datetime field being used). Even when the values appear to be exactly the same PHP still thinks they are different. Could anyone shed any light on what I am doing wrong?
The following gives me the date in the correct format: "2011-04-22 18:36:00" which is being generated from a date picker and 2 seperate text boxes for the hour and minute.
if(isset($_POST['enableReminder'])) {
$_POST['reminder_date'] = str_replace("/","",$_POST['reminder_date']);
$day3 = substr($_POST['reminder_date'],0,2);
$month3 = substr($_POST['reminder_date'],2,2);
$year3 = substr($_POST['reminder_date'],-4);
$taskReminderDate = $year3.'-'.$month3.'-'.$day3;
$taskReminderHour = $_POST['reminder_hour'];
$taskReminderMinute = $_POST['reminder_minute'];
$taskReminderDBpre = $taskReminderDate.' '.$taskReminderHour.':'.$taskReminderMinute.':00';
$taskReminderDB = date( 'Y-m-d H:i:s', strtotime($taskReminderDBpre) );
$taskReminderDB2 = "'".$taskReminderDB."'";
} else {
$taskReminderDB = 'NULL';
}
I then try to compare what's in the database, and if the values are different set a variable accordingly:
$taskReminderCompare = strtotime($row['reminder_date']);
if($taskReminderDB !== $taskReminderCompare) {
$taskReminderSent = 0;
} else {
$taskReminderSent = 1;
}
But it ALWAYS returns 0 even if I echo $taskReminderDB and $taskReminderCompare and they are exactly the same.
Upvotes: 2
Views: 2097
Reputation: 401122
It seems your $taskReminderDB
variable is created like this :
$taskReminderDB = date( 'Y-m-d H:i:s', strtotime($taskReminderDBpre) );
Which means it'll contain a string -- a date formated as YYYY-MM-DD hh:mm:ss
$taskReminderCompare = strtotime($row['reminder_date']);
Which means it'll contain a UNIX Timestamp -- which is an integer.
To be sure, you could use the var_dump()
function to get some informations (value and type) about what's in those :
var_dump($taskReminderDB, $taskReminderCompare);
i.e. the 123456
integer is not equal to the '123456'
string, if comparing them with !==
Upvotes: 3