How to get Time Difference in Minutes in PHP ?

26 May, 2021
In this article, we will learn how to get time difference in minutes using PHP. 
We will be using the built-in function date_diff() to get the time difference in minutes. For this, we will be needed a start date and end date to calculate their time difference in minutes using the date_diff() function.
Syntax:
date_diff($datetime1, $datetime2);
Parameters: The date_diff() function accepts two parameters as mentioned above and described below.
$datetime1: This is a mandatory parameter as it specifies the start/first DateTime object.
$datetime2: This is a mandatory parameter as it specifies the end/second DateTime object.
Return Value: This function returns the difference between the first DateTime object and the second DateTime object otherwise it returns false on failure.
Example 1: The below program illustrates the date_diff() function to get the time difference in minutes.
PHP
<?php // PHP Program to illustrate//date_diff() function    // Creating DateTime Objects$dateTimeObject1 = date_create('2019-05-18'); $dateTimeObject2 = date_create('2020-05-18');     // Calculating the difference between DateTime Objects$interval = date_diff($dateTimeObject1, $dateTimeObject2); echo ("Difference in days is: ");  // Printing the result in days formatecho $interval->format('%R%a days');echo "\n<br/>";$min = $interval->days * 24 * 60;$min += $interval->h * 60;$min += $interval->i;  // Printing the Result in Minutes format.echo("Difference in minutes is: ");echo $min.' minutes';?>
Output:
Difference in days is: +366 days
Difference in minutes is: 527040 minutes
Example 2:
PHP
<?php   // PHP Program to illustrate  // date_diff() function    // Creating DateTime Objects  $dateTimeObject1 = date_create('2020-05-14');   $dateTimeObject2 = date_create('2021-02-14');     // Calculating the difference between DateTime Objects  $interval = date_diff($dateTimeObject1, $dateTimeObject2);   echo ("Difference in days is: ");    // Printing the result in days format  echo $interval->format('%R%a days');  echo "\n<br/>";  $min = $interval->days * 24 * 60;  $min += $interval->h * 60;  $min += $interval->i;    // Printing the Result in Minutes format.  echo("Difference in minutes is: ");  echo $min.' minutes';?>
Output:
Difference in days is: +276 days
Difference in minutes is: 397440 minutes
Example 3:
PHP
<?php // PHP program to illustrate // date_diff() function    // Creating DateTime objects$dateTimeObject1 = date_create('19:15:00'); $dateTimeObject2 = date_create('12:15:00');   // Calculating the difference between DateTime objects$interval = date_diff($dateTimeObject1, $dateTimeObject2);   // Printing result in hoursecho ("Difference in hours is:");echo $interval->h;echo "\n<br/>";$minutes = $interval->days * 24 * 60;$minutes += $interval->h * 60;$minutes += $interval->i;  //Printing result in minutesecho("Difference in minutes is:");echo $minutes.' minutes';?>
Output:
Difference in hours is:7
Difference in minutes is:420 minutes
PHP-date-time
PHP-function
PHP-Questions
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to delete an array element based on key in PHP?
PHP in_array() Function
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript
How to fetch data from an API in ReactJS ?