How to get the difference between two days in php

I have a function passing some parameter like

everyWeekOn("Mon",11,19,00)

      

I want to calculate the difference between the current day (eg "Fri") and the parameter day passed, i.e. Mon

...

The output should be:

The difference between Mon and Fri is 3

      

I tried this like

 $_dt = new DateTime();
 error_log('$_dt date'. $_dt->format('d'));
 error_log('$_dt year'. $_dt->format('Y'));
 error_log('$_dt month'. $_dt->format('m'));

      

But I know that I don't know what to do next to get the difference between two days.

Note that this question is different from How do I calculate the difference between two dates using PHP? because I only have a day and not a full date.

+3


source to share


3 answers


Just implement the class DateTime

in conjunction with the method ->diff

:



function everyWeekOn($day) {
    $today = new DateTime;
    $next = DateTime::createFromFormat('D', $day);
    $diff = $next->diff($today);
    return "The difference between {$next->format('l')} and {$today->format('l')} is {$diff->days}";
}

echo everyWeekOn('Mon');

      

+3


source


$date = new DateTime('2015-01-01 12:00:00');
$difference = $date->diff(new DateTime());
echo $difference->days.' days <br>';

      



+1


source


You may find no. days two days later using this code

<?php
    $today = time(); 
    $chkdate = strtotime("16-04-2015");
    $date = $today - $chkdate;
    echo floor($date/(60*60*24));
?>

      

Please use this, maybe it will help you

0


source







All Articles