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

php difference between two dates

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days
", $years, $months, $days);
Comment

difference entre deux date 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

Calculate the Difference Between Two Dates Using PHP

phpCopy$firstDate = "2019-01-01";
$secondDate = "2020-03-04";

$dateDifference = abs(strtotime($secondDate) - strtotime($firstDate));

$years  = floor($dateDifference / (365 * 60 * 60 * 24));
$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days   = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 *24) / (60 * 60 * 24));

echo $years." year,  ".$months." months and ".$days." days";

//output: 1 year, 2 months and 3 days
Comment

PREVIOUS NEXT
Code Example
Php :: laravel foreach 
Php :: laravel migrate refresh specific migration 
Php :: $errors show this error in laravel 
Php :: brew downgrade php 8 to 7.4 
Php :: php pdo database connection 
Php :: laravel unsigned integer 
Php :: upload file in php 
Php :: download file php 
Php :: change password function in laravel 
Php :: session cakephp 
Php :: Theme and plugin editor in wp dashboard missing 
Php :: laravel username validation 
Php :: destroy all sessions in laravel 
Php :: php array remove key value pair 
Php :: laravel 8: bootstrap 
Php :: pluck array in laravel 
Php :: php get url parameter 
Php :: implode and explode in php 
Php :: create seeder in laravel 
Php :: mysqli_real_connect(): (HY000/2002): No such file or directory 
Php :: delete file in php 
Php :: define in php 
Php :: search by date using carbon laravel 
Php :: how to get product id by sku in woocommerce 
Php :: trim array in map php 
Php :: create storage link laravel without terminal server 
Php :: Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1 
Php :: laravel where and or condition 
Php :: laravel validation unique email except self 
Php :: php Calculate the number of months between two dates 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =