I keep getting a value of 0 for $years and $months when there should be a value other then 0 can someone tell me what I’m doing wrong with my code? And what I need to fix?
I’m trying to subtract two dates.
Here is my PHP code.
$delete_date = "2000-01-12 08:02:39";
$current_date = date('Y-m-d H:i:s'); //current date
$diff = abs(strtotime($current_date) - strtotime($delete_date));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
echo $current_date . '<br />';
echo $delete_date. '<br />';
echo $diff. '<br />';
echo $years. '<br />';
echo $months. '<br />';
You should try simply using the PHP function
DateTime::diff()
also known as
date_diff()
This will give you the difference in time which you can more easily parse into months and years.
Read this for more information:
http://www.php.net/manual/en/datetime.diff.php