Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php calculate date difference

//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 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

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

difference of two dates in seconds php

$timeFirst  = strtotime('2011-05-12 18:20:20');
$timeSecond = strtotime('2011-05-13 18:20:20');
$differenceInSeconds = $timeSecond - $timeFirst;
Comment

How to calculate the difference between two dates php

$datetime1 	= new DateTime('2020-10-11 16:52:52');
$datetime2 	= new DateTime('2020-10-13 16:52:52');
$interval 	= $datetime1->diff($datetime2);

echo $interval->format('%a days');

$interval = $datetime1->diff($datetime2);
$diffInSeconds = $interval->s; 
$diffInMinutes = $interval->i; 
$diffInHours   = $interval->h; 
$diffInDays    = $interval->d; 
$diffInMonths  = $interval->m; 
$diffInYears   = $interval->y; 
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

php difference between two dates in seconds

$start = new DateTime('2011-12-31 00:00:00');
$end = new DateTime('2021-01-01 00:00:00');

echo $end->getTimestamp() - $start->getTimestamp(); // output: 284169600
Comment

PREVIOUS NEXT
Code Example
Php :: flutter form set next input 
Php :: php array get first x elements 
Php :: The mysqli extension is missing. Please check your PHP configuration. 
Php :: carbon parse subday 
Php :: wordpress thumbnail url 
Php :: change php version linux nginx 
Php :: get all pages list from specific template 
Php :: laravel mix disable notifications 
Php :: php get filename without extension 
Php :: pause php 
Php :: php get extension from string 
Php :: laravel change column type 
Php :: http error 500 phpmyadmin 
Php :: php get remote file last modified 
Php :: pdo fetch 
Php :: laravel not finding asset files in public directory 
Php :: ext-dom php 7.2 
Php :: php alert 
Php :: php regex remove characters from string 
Php :: laravel validation integer 
Php :: excerpt length wordpress 
Php :: file original extensions laravel 
Php :: php sleep milliseconds 
Php :: 2 chiffres apres virgule php 
Php :: remove slashes from json php 
Php :: use wordpress functions in external php file 
Php :: remove index.php from url htaccess 
Php :: laravel cashier overwrite users table name 
Php :: destroy php variable 
Php :: php object to xml 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =