Open In App

How to get Time Difference in Minutes in PHP ?

Last Updated : 26 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 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: +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 hours
echo ("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 minutes
echo("Difference in minutes is:");
echo $minutes.' minutes';
?>


Output:

Difference in hours is:7
Difference in minutes is:420 minutes


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads