Find the difference between 2 dates in days with PHP

Need to find the number of days between 2 dates with PHP in an older version of PHP? The below code will work fine in versions of PHP 5 and below.

<?php
function date_diff_in_days($from, $to = 'now')
{
    $date1 = strtotime($from);
    $date2 = strtotime($to);
    $dateDiff = $date1 - $date2;
    return floor($dateDiff/(60*60*24));
}
?>

Justin Kelly

Justin Kelly

Web Developer, Business Analytics, Data Engineer specialising in PHP, ReactJS, Tableau, AWS RedShift and Matillion ETL

Based in Melbourne, Australia

Feel free to contact me justin@kelly.org.au or _justin_kelly

Leave a comment

Name Notify me of replies by email.