PHP Calculate Time Difference Between Two Dates in Hours Example

By Hardik Savani May 14, 2024 Category : PHP

In this article we will cover on how to implement php calculate time difference between two dates in hours. you'll learn php difference between two dates in hours. This tutorial will give you simple example of how to calculate time difference between two dates in php. I’m going to show you about how to find difference between two dates in hour in php. You just need to some step to done php calculate time difference between two dates in hours.

Let's see simple example of how to get difference between two dates in hours with php.

Example:

<?php

$date1 = "2021-09-05 01:00:00";

$date2 = "2021-09-06 02:00:00";

$timestamp1 = strtotime($date1);

$timestamp2 = strtotime($date2);

$hours = abs($timestamp2 - $timestamp1)/(60*60);

echo "Difference between two dates is " . $hours . " hour(s)";

Output:

Difference between two dates is 25 hour(s)

i hope it can help you...

Tags :
Shares