The Web Blinders logo

Programming

PHP getting difference between two dates in days

<?php
function differenceInDays($date1, $date2)
{
    // change $format
    // as per your server 
    // date format 
    $format='Y-m-d';
    $diff = DateTime::CreateFromFormat($format, $date2)->diff(DateTime::CreateFromFormat($format, $date1));
    return $diff->format('%a')." "."day(s)";
}
// 30 day(s)
var_dump(differenceInDays('2015-06-11', '2015-07-11'));
// 92 day(s)
var_dump(differenceInDays('2019-05-22', '2019-08-22'));
// 2 day(s)
var_dump(differenceInDays('2019-06-23', '2019-06-25'));

differenceInDays() comes handy for many operations on php dates.

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES