I need to get the number of years from 2 dates provided. Here's my code:
function daysDifference($endDate, $beginDate) { $date_parts1=explode("-", $beginDate); $date_parts2=explode("-", $endDate); $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); $diff = $end_date - $start_date; echo $diff; $years = floor($diff / (365.25*60*60*24)); return $years; } echo daysDifference('2011-03-12','2008-03-09');
The $diff
gives a number output. When I return $years
, am getting 0
. What have I done wrong?
The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
php $start = strtotime("12:00"); $end = // Run query to get datetime value from db $elapsed = $end - $start; echo date("H:i", $elapsed); ?>
$d1 = new DateTime('2011-03-12'); $d2 = new DateTime('2008-03-09'); $diff = $d2->diff($d1); echo $diff->y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With