How to get Time Difference in Minutes in PHP ? (original) (raw)

Last Updated : 23 Jul, 2025

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()toget 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.

**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 `

interval=datediff(interval = date_diff(interval=datediff(dateTimeObject1, $dateTimeObject2); echo ("Difference in days is: "); // Printing the result in days format echo $interval->format('%R%a days'); echo "\n
"; min=min = min=interval->days * 24 * 60; min+=min += min+=interval->h * 60; min+=min += 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 `

interval=datediff(interval = date_diff(interval=datediff(dateTimeObject1, $dateTimeObject2); echo ("Difference in days is: "); // Printing the result in days format echo $interval->format('%R%a days'); echo "\n
"; min=min = min=interval->days * 24 * 60; min+=min += min+=interval->h * 60; min+=min += 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 `

interval=datediff(interval = date_diff(interval=datediff(dateTimeObject1, $dateTimeObject2); // Printing result in hours echo ("Difference in hours is:"); echo $interval->h; echo "\n
"; minutes=minutes = minutes=interval->days * 24 * 60; minutes+=minutes += minutes+=interval->h * 60; minutes+=minutes += 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

Using strtotime() function

Another approach to calculate the time difference in minutes between two dates is by using the strtotime() function. This method involves converting the date strings into Unix timestamps and then calculating the difference.

**Example:

PHP `

starttimestamp=strtotime(start_timestamp = strtotime(starttimestamp=strtotime(start_date); endtimestamp=strtotime(end_timestamp = strtotime(endtimestamp=strtotime(end_date); // Calculate the difference in seconds differenceinseconds=abs(difference_in_seconds = abs(differenceinseconds=abs(end_timestamp - $start_timestamp); // Convert the difference from seconds to minutes differenceinminutes=difference_in_minutes = differenceinminutes=difference_in_seconds / 60; return $difference_in_minutes; } // Example usage $start_date = "2023-07-01 12:00:00"; $end_date = "2024-07-02 12:00:00"; echo "Difference in minutes is: " . getTimeDifferenceInMinutes($start_date, $end_date) . " minutes"; ?>

`

Output

Difference in minutes is: 528480 minutes