Search
 
SCRIPT & CODE EXAMPLE
 

PHP

datetime difference in php

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

Use DateTime() and DateInterval() Objects for PHP 5.3 and Above and Calculate the Difference Between Two Dates Using PHP

phpCopy$firstDate  = new DateTime("2019-01-01");
$secondDate = new DateTime("2020-03-04");
$intvl = $firstDate->diff($secondDate);

echo $intvl->y . " year, " . $intvl->m." months and ".$intvl->d." day"; 
echo "
";
// Total amount of days
echo $intvl->days . " days ";

//output: 1 year, 2 months and 1 day
//        428 days
Comment

PREVIOUS NEXT
Code Example
Php :: codeigniter crud generator 
Php :: php pre 
Php :: filter value in array php return single value 
Php :: laravel 8 logout 419 page expired 
Php :: input if not null laravel 
Php :: Laravel - multiple select query 
Php :: php array_map 
Php :: stored procedure laravel 
Php :: php page sends cookie to visitor 
Php :: cURL error 6: Could not resolve host: api.themoviedb.org (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.themoviedb.org/3/movie/popular?api_key=5cb73b68870b70a436b10ea06298de07 
Php :: how to store wp editor in wordpress 
Php :: -with() in laravel 
Php :: php array_push 
Php :: PHP Custom Time Ago Function 
Php :: placeholder for select php 
Php :: csv import in laravel 
Php :: how to convert amount in words in php 
Php :: validation.required laravel 
Php :: Wampserver does not use, modify or require the PATH environment variable. 
Php :: membuat aplikasi dengan array dalam bahasa pemrograman PHP 
Php :: echo two variables same line php 
Php :: unable to composer require apidoc yii2 
Php :: PHP utf8_decode — Converts a string from UTF-8 to ISO-8859-1, replacing invalid or unrepresentable characters 
Php :: data types of laravel migrations 
Php :: Header requirements for new plugin in wordpress 
Php :: razorpay refund laravel 
Php :: Agregar clases de rol al body en WordPress 
Php :: dorks 
Php :: requires ext-pcntl 
Php :: controller run 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =