Difference between 2 dates using PHP
hi,
having issues calculating difference between 2 dates.
i have noticed around half of results output 552 months... should around 24 months...
i have noticed records display incorrect calculation have months of date less current month, sets $date2 01/01/1970
this code:
<?php
//calculate months remaining
$date1 = date('d/m/y');
$date2 = $renewal_row['enddate'];
//$date2 = date('d/m/y', strtotime(str_replace('.', '-', $renewal_row['enddate'])));
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//printf("%d years, %d months, %d days\n", $years, $months, $days);
$months_remaining = ($years*12) + $months;
?>
<?php echo '$date1='.strtotime($date1).'('.$date1.') date2='.strtotime($date2).'('.$date2.') difference='.$diff; ?>
date correct difference between both dates:
$date1=1452038400(01/06/2016) date2=1520035200(03/03/2018) difference=67996800
date incorrect difference:
$date1=1452038400(01/06/2016) date2=(20/11/2016) difference=1452038400
notice if month in $date2 greater current month (june) messes up.
any appreciated!
you start right off defining dates in format d/m/y. that's first mistake. whole approach wrong.
use php datetime class. thing of beauty. 1 of nice things keeps track of date date , return formatted value when need display date web page.
david powers has thorough tutorial on php dates @ lynda.com. highly recommend it.
by way, 1/1/1970 php returns when can't parse date.
More discussions in Dreamweaver support forum
adobe
Comments
Post a Comment