To get PHP Date Time Difference in Days, Hours, Minutes, and Seconds we use three major PHP built-in things DateTime object, date_create() function, and date_diff() function.
We also use DateTimeZone object to get specific timezone date and time and calculate differnce between two dates.
Introduction
date_create() function is used get DateTime object and also you can format date and time accordingly.
Syntax:
1 2 3 4 |
<?php $date=date_create("2021-04-25"); echo date_format($date,"Y/m/d"); ?> |
Output: 2021/04/25
date_diff() function is helps to get difference between two DateTime object.
Syntax:
1 2 3 4 5 |
<?php $date1=date_create("2021-04-25"); $date2=date_create("2019-12-12"); $diff=date_diff($date1,$date2); ?> |
Output:
DateInterval Object ( [y] => 1 [m] => 4 [d] => 13 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 500 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
PHP Date Time Difference in Days, Hours, Minutes, and Seconds With Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php //if you leave this it takes your current timezone $userTimezone = "America/Los_Angeles"; $timezone = new DateTimeZone( $userTimezone ); $crrentSysDate = new DateTime(date('m/d/y h:i:s a'),$timezone); $userDefineDate = $crrentSysDate->format('m/d/y h:i:s a'); $start = date_create($userDefineDate,$timezone); $end = date_create(date('m/d/y h:i:s a', strtotime('10/20/19 10:20:35')),$timezone); $diff=date_diff($start,$end); echo "Year: ".$diff->y."</br>"; echo "Month: ".$diff->m."</br>"; echo "Days: ".$diff->d."</br>"; echo "Hours: ".$diff->h."</br>"; echo "Minutes: ".$diff->i."</br>"; echo "Seconds: ".$diff->s; ?> |
To compile it go there https://codingtasks.net/php-online-editor/ and copy paste the above code.
Output:
Year: 1
Month: 6
Days: 6
Hours: 7
Minutes: 37
Seconds: 39
Code Highlights:
- On the first line, we set the time zone using
DateTimeZone( $userTimezone )
object and if you do not specify it, it takes the default time zone. - Then we get the current time of the specified time zone by using
DateTime(date('m/d/y h:i:s a'),$timezone)
object. - After that, we get our start date and end date using
date_create()
function for the specific format to get the difference. - Now we get the difference between two dates in PHP by using the date_diff() function.
I hope you got it completely.
To know more about PHP date and date difference you can check https://www.php.net/manual/en/function.date-diff.php
Also Check:
- Get Number of Days Between Two Dates Using JS and jQuery
- Bootstrap Datepicker- Complete Solution to Integrate
- Set DateTime With FileName While Uploading in PHP
- How to Set PHP Variable in JavaScript With Example
Happy Coding..!
[…] Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds […]
[…] Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds […]
[…] Also Read: Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds […]
[…] Also Read: Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds […]
[…] Get PHP Date Time Difference in Days, Hours, Minutes, and Seconds […]