Wednesday, 7 January 2015

date time difference in php

function datediff( $date1, $date2 )
{
    $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );

    return sprintf
    (
        "%d Days, %d Hours, %d Mins, %d Seconds",
        intval( $diff / 86400 ),
        intval( ( $diff % 86400 ) / 3600),
        intval( ( $diff / 60 ) % 60 ),
        intval( $diff % 60 )
    );
}

print datediff( "18th February 2013", "now" ) . "\n";



$start_date = new DateTime("2012-02-10 11:26:00");
    $end_date = new DateTime("2012-04-25 01:50:00");
    $interval = $start_date->diff($end_date);
    echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";

No comments:

Post a Comment