- How to Get Time Difference Between Two DateTimes in Minutes using PHP
- date_diff
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes
- Php time difference between
- Get Time Difference in Minutes in PHP
- Use date_diff() Function to Get Time Difference in Minutes in PHP
- Use Mathematical Formulas to Get Time Difference in Minutes in PHP
- Related Article — PHP Time
How to Get Time Difference Between Two DateTimes in Minutes using PHP
There are two ways to get the time difference in minutes with PHP.
1. Use the PHP DateTime class to calculate the difference between two date-times. The diff() method of DateTime class creates a DateInterval object that calculates the difference between two date/time objects in time (total days, years, months, days, hours, minutes, seconds, etc.).
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$start_datetime = new DateTime($datetime_1);
$diff = $start_datetime->diff(new DateTime($datetime_2));
echo $diff->days.' Days total
';
echo $diff->y.' Years
';
echo $diff->m.' Months
';
echo $diff->d.' Days
';
echo $diff->h.' Hours
';
echo $diff->i.' Minutes
';
echo $diff->s.' Seconds
';
Calculate and get the time difference in minutes:
$total_minutes = ($diff->days * 24 * 60);
$total_minutes += ($diff->h * 60);
$total_minutes += $diff->i;
echo 'Diff in Minutes: '.$total_minutes;
2. You can use strtotime() function to get the time difference between two dates (DateTime) in minutes using PHP.
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$from_time = strtotime($datetime_1);
$to_time = strtotime($datetime_2);
$diff_minutes = round(abs($from_time - $to_time) / 60,2). " minutes";
date_diff
Returns the difference between two DateTimeInterface objects.
Parameters
Should the interval be forced to be positive?
Return Values
The DateInterval object represents the difference between the two dates.
The return value more specifically represents the clock-time interval to apply to the original object ( $this or $originObject ) to arrive at the $targetObject . This process is not always reversible.
The method is aware of DST changeovers, and hence can return an interval of 24 hours and 30 minutes , as per one of the examples. If you want to calculate with absolute time, you need to convert both the $this / $baseObject , and $targetObject to UTC first.
Examples
Example #1 DateTimeImmutable::diff() example
$origin = new DateTimeImmutable ( ‘2009-10-11’ );
$target = new DateTimeImmutable ( ‘2009-10-13’ );
$interval = $origin -> diff ( $target );
echo $interval -> format ( ‘%R%a days’ );
?>?php
$origin = date_create ( ‘2009-10-11’ );
$target = date_create ( ‘2009-10-13’ );
$interval = date_diff ( $origin , $target );
echo $interval -> format ( ‘%R%a days’ );
?>?php
The above examples will output:
Example #2 DateTimeInterface::diff() during DST changeover
$originalTime = new DateTimeImmutable ( «2021-10-30 09:00:00 Europe/London» );
$targedTime = new DateTimeImmutable ( «2021-10-31 08:30:00 Europe/London» );
$interval = $originalTime -> diff ( $targedTime );
echo $interval -> format ( «%H:%I:%S (Full days: %a)» ), «\n» ;
?>?php
The above example will output:
Example #3 DateTime object comparison
$date1 = new DateTime ( «now» );
$date2 = new DateTime ( «tomorrow» );
?php
var_dump ( $date1 == $date2 );
var_dump ( $date1 < $date2 );
var_dump ( $date1 > $date2 );
?>
The above example will output:
bool(false) bool(true) bool(false)
See Also
- DateInterval::format() — Formats the interval
- DateTime::add() — Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
- DateTime::sub() — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
User Contributed Notes
Php time difference between
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
Get Time Difference in Minutes in PHP
- Use date_diff() Function to Get Time Difference in Minutes in PHP
- Use Mathematical Formulas to Get Time Difference in Minutes in PHP
In this article, we will introduce methods to get time difference in minutes in PHP.
Use date_diff() Function to Get Time Difference in Minutes in PHP
We will use the built-in function date_diff() to get time difference in minutes.
For this, we need a start date and an end date. We will calculate their time difference in minutes using the date_diff() function. The correct syntax to use this function is as follows.
date_diff($DateTimeObject1, $DateTimeObject2);
This function returns the difference between the start date and end date on success. It returns FALSE on failure.
The program below shows how we can use the date_diff() function to get time difference in minutes.
php $dateTimeObject1 = date_create('2019-06-16'); $dateTimeObject2 = date_create('2020-06-16'); $difference = date_diff($dateTimeObject1, $dateTimeObject2); echo ("The difference in days is:"); echo $difference->format('%R%a days'); echo "\n"; $minutes = $difference->days * 24 * 60; $minutes += $difference->h * 60; $minutes += $difference->i; echo("The difference in minutes is:"); echo $minutes.' minutes'; ?>
The function date_diff() has returned an object that represents the difference between two dates.
The difference in days is:+366 days The difference in minutes is:527040 minutes
Now we will find the difference between time.
php $dateTimeObject1 = date_create('17:13:00'); $dateTimeObject2 = date_create('12:13:00'); $difference = date_diff($dateTimeObject1, $dateTimeObject2); echo ("The difference in hours is:"); echo $difference->h; echo "\n"; $minutes = $difference->days * 24 * 60; $minutes += $difference->h * 60; $minutes += $difference->i; echo("The difference in minutes is:"); echo $minutes.' minutes'; ?>
The difference in hours is:5 The difference in minutes is:300 minutes
Use Mathematical Formulas to Get Time Difference in Minutes in PHP
In PHP, we can also use different mathematical formulas to get time difference in minutes. The program that gets time difference in minutes is as follows:
php $to_time = strtotime("10:42:00"); $from_time = strtotime("10:21:00"); $minutes = round(abs($to_time - $from_time) / 60,2); echo("The difference in minutes is: $minutes minutes."); ?>
The difference in minutes is: 21 minutes
We can also find the time difference in minutes using the following method.
php $start = strtotime('12:01:00'); $end = strtotime('13:16:00'); $minutes = ($end - $start) / 60; echo "The difference in minutes is $minutes minutes."; ?>
The difference in minutes is 75 minutes.