I have a DateTime Object and I want to get the differnce between this and the current date. But I always get ZERO as my differnce, but that cannot be. Can't find the answer. Could anyone help me? THANK YOU!
private function checkAge($date) {
$currentDate = date('Y-m-d H:i:s');
$result = $date->format('Y-m-d H:i:s');
$diffDate = ($currentDate-$result);
echo $diffDate;
if ($diffDate>=43200) {
return true;
}
return false;
}
$currentDate
and $result
are both strings. Subtracting them is not going to give the difference between the two dates.
You can either use the DateTime diff method or, if you just need the seconds you can compare timestamps:
private function checkAge($date) {
$currentDate = date('U');
$result = $date->format('U');
$diffDate = ($currentDate-$result);
echo $diffDate;
if ($diffDate>=43200) {
return true;
}
return false;
}
Have you tried the the diff function to compare dates ?
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>