Wednesday, March 4, 2015

calculate the difference between two dates using PHP?

Standard
You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods


            $date1 = "2015-03-08";
            $date2 = date("Y-m-d");

            $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);
            printf(" %d months, %d days Days Ago\n", $months, $days);
            printf("%d Days Ago\n",  $days);

0 comments:

Post a Comment