Calculate time difference total hours minutes months years

0.62K views
no comments
12 Dec 2014 8:27 pm

I developed my custom code to get difference between two dates, code is different from #php date_diff because my code calculate the total number of seconds, minutes, days,  months and years.

Its best way to define this code in function to use any where of project where you want it, at the end of post I submitted time function.

1.Define function with named date_time_distance and pass three parameters

function date_time_distance($start_time = "", $end_time = "",$return_array=false){
}

2.Get the current time if second parameter $end_time not passed or null

    if ($end_time == "") {
        $unix_end_time = current_time('timestamp', $gmt = 0);
    } else {
        $unix_end_time = strtotime($end_time);
    }

3.Now calculate the different of time from unix timestamp

if ($start_time != "" || $unix_start_time < $unix_end_time) {
        $second_diff = $unix_end_time - $unix_start_time;
        $minuts_diff = round($second_diff / 60);
        $hours_diff = round($minuts_diff / 60);
        $days_diff = round($hours_diff / 24);
        $months_diff = round($days_diff / 30);
        $years_diff = round($months_diff / 12);
}

4.Define associative array of time differences

        $time_diff = array();
        $time_diff['seconds_diff'] = $second_diff;
        $time_diff['minutes_diff'] = $minuts_diff;
        $time_diff['hours_diff'] = $hours_diff;
        $time_diff['days_diff'] = $days_diff;
        $time_diff['months_diff'] = $months_diff;
        $time_diff['years_diff'] = $years_diff;

5.Find the default echo value

Like the time difference is 1 second it will return to 1 second, You don’t need IF Statement anywhere you call it.

        if ($time_diff['years_diff'] != "") {
            $datetime_diff = $time_diff['years_diff'] > 1 ? $time_diff['years_diff'] . " years" : $time_diff['years_diff'] . " year";
        } else if ($time_diff['months_diff'] != "") {
            $datetime_diff = $time_diff['months_diff'] > 1 ? $time_diff['months_diff'] . " months" : $time_diff['months_diff'] . " month";
        } else if ($time_diff['days_diff'] != "") {
            $datetime_diff = $time_diff['days_diff'] > 1 ? $time_diff['days_diff'] . " days" : $time_diff['days_diff'] . " today";
        } else if ($time_diff['hours_diff'] != "") {
            $datetime_diff = $time_diff['hours_diff'] > 1 ? $time_diff['hours_diff'] . " hours" : $time_diff['hours_diff'] . " hour";
        } else if ($time_diff['minutes_diff'] != "") {
            $datetime_diff = $time_diff['minutes_diff'] > 1 ? $time_diff['minutes_diff'] . " minutes" : $time_diff['minutes_diff'] . " minute";
        } else if ($time_diff['seconds_diff'] != "") {
            $datetime_diff = $time_diff['seconds_diff'] > 1 ? $time_diff['seconds_diff'] . " seconds" : $time_diff['seconds_diff'] . " second";
        }

        $time_diff['datetime_diff'] = $datetime_diff;

6.Check condition what third parameter passed for return array or value

        if($return_array===true){
            return $time_diff;
        } else {
            return $time_diff['datetime_diff'];
        }

Here is the full code of function date_time_distance

function date_time_distance($start_time = "", $end_time = "",$return_array=false){

    if ($end_time == "") {
        $unix_end_time = current_time('timestamp', $gmt = 0);
    } else {
        $unix_end_time = strtotime($end_time);
    }

    $unix_start_time = strtotime($start_time);

    if ($start_time != "" || $unix_start_time < $unix_end_time) {
        $second_diff = $unix_end_time - $unix_start_time;
        $minuts_diff = round($second_diff / 60);

        $hours_diff = round($minuts_diff / 60);
        $days_diff = round($hours_diff / 24);
        $months_diff = round($days_diff / 30);
        $years_diff = round($months_diff / 12);

        $time_diff = array();
        $time_diff['seconds_diff'] = $second_diff;
        $time_diff['minutes_diff'] = $minuts_diff;
        $time_diff['hours_diff'] = $hours_diff;
        $time_diff['days_diff'] = $days_diff;
        $time_diff['months_diff'] = $months_diff;
        $time_diff['years_diff'] = $years_diff;

        if ($time_diff['years_diff'] != "") {
            $datetime_diff = $time_diff['years_diff'] > 1 ? $time_diff['years_diff'] . " years" : $time_diff['years_diff'] . " year";
        } else if ($time_diff['months_diff'] != "") {
            $datetime_diff = $time_diff['months_diff'] > 1 ? $time_diff['months_diff'] . " months" : $time_diff['months_diff'] . " month";
        } else if ($time_diff['days_diff'] != "") {
            $datetime_diff = $time_diff['days_diff'] > 1 ? $time_diff['days_diff'] . " days" : $time_diff['days_diff'] . " today";
        } else if ($time_diff['hours_diff'] != "") {
            $datetime_diff = $time_diff['hours_diff'] > 1 ? $time_diff['hours_diff'] . " hours" : $time_diff['hours_diff'] . " hour";
        } else if ($time_diff['minutes_diff'] != "") {
            $datetime_diff = $time_diff['minutes_diff'] > 1 ? $time_diff['minutes_diff'] . " minutes" : $time_diff['minutes_diff'] . " minute";
        } else if ($time_diff['seconds_diff'] != "") {
            $datetime_diff = $time_diff['seconds_diff'] > 1 ? $time_diff['seconds_diff'] . " seconds" : $time_diff['seconds_diff'] . " second";
        }

        $time_diff['datetime_diff'] = $datetime_diff;
        if($return_array===true){
            return $time_diff;
        } else {
            return $time_diff['datetime_diff'];
        }
        

    }
}

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>