get the differnces between two date and time in hours and minutes in php code example

Example 1: get hours difference between two dates in php

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

Example 2: php time difference in hours

date_default_timezone_set("Africa/Johannesburg");
    $now = new DateTime();
    $future_date = new DateTime('2020-10-21 00:00:00');
    
    $interval = $future_date->diff($now);
    
    echo ($interval->format("%a") * 24) + $interval->format("%h"). " hours". $interval->format(" %i minutes ");
    print_r($now->format('Y-m-d H:i:s'));

Example 3: how to calculate days difference between two dates in php

// how to calculate days difference between two dates in laravel

use DateTime; // inside Controller Class

$startDate = new DateTime($request->start_date);
$endDate   = new DateTime($request->end_date);

$daysDifference = ($startDate->diff($endDate)->days);

Tags:

Php Example