Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds

//get Date diff as intervals 
$d1 = new DateTime("2018-01-10 00:00:00");
$d2 = new DateTime("2019-05-18 01:23:45");
$interval = $d1->diff($d2);
$diffInSeconds = $interval->s; //45
$diffInMinutes = $interval->i; //23
$diffInHours   = $interval->h; //8
$diffInDays    = $interval->d; //21
$diffInMonths  = $interval->m; //4
$diffInYears   = $interval->y; //1

//or get Date difference as total difference
$d1 = strtotime("2018-01-10 00:00:00");
$d2 = strtotime("2019-05-18 01:23:45");
$totalSecondsDiff = abs($d1-$d2); //42600225
$totalMinutesDiff = $totalSecondsDiff/60; //710003.75
$totalHoursDiff   = $totalSecondsDiff/60/60;//11833.39
$totalDaysDiff    = $totalSecondsDiff/60/60/24; //493.05
$totalMonthsDiff  = $totalSecondsDiff/60/60/24/30; //16.43
$totalYearsDiff   = $totalSecondsDiff/60/60/24/365; //1.35
Comment

get hours difference between two dates in php

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

calculate time difference php

$time1 = new DateTime('09:00:59');
$time2 = new DateTime('09:01:00');
$interval = $time1->diff($time2);
echo $interval->format('%s second(s)');

Result: 1 second(s)
Comment

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'));
Comment

php calculate hours and minutes between two times

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'));
Comment

PREVIOUS NEXT
Code Example
Php :: current date time in php 
Php :: php post 
Php :: wp enqueue style 
Php :: check if url is https laravel 
Php :: Laravel Auth Redirect based on role 
Php :: mt_rand 
Php :: how to get the average in sql in php 
Php :: How To Force Redirect HTTP To HTTPS In Laravel Using htaccess 
Php :: laravel update and insert transaction 
Php :: how to run php file in xampp 
Php :: laravellivewire is not defined 
Php :: SELECT query with PDO 
Php :: php superglobal 
Php :: Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php 
Php :: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65015808 bytes) 
Php :: slug in php 
Php :: check if the form is submitted php 
Php :: laravel return json header json 
Php :: php get text from html 
Php :: laravel join 
Php :: remove all items of an array except the last one in php 
Php :: getting last day of next month in php 
Php :: wc php get product category in product page 
Php :: laravel limit relationship result 
Php :: laravel share on whatsapp link 
Php :: convert xml file to array php 
Php :: laravel set config value runtime 
Php :: json stringify php decode 
Php :: pass parameter to view laravel 
Php :: Remove public or index file from url in laravel 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =