ItSolutionStuff.com

PHP Calculate Time Difference Between Two Dates in Hours Example

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Laravel Carbon Subtract Days to Date Example

Read Now →
ā˜…

Angular Change Date Format in Component Example

Read Now →
ā˜…

How to Get Current Date and Time in AngularJS?

Read Now →
ā˜…

How to Change Date Format in Codeigniter?

Read Now →
ā˜…

Bootstrap Timepicker using Datetimepicker JS Example

Read Now →
ā˜…

Laravel Change Date Format using Carbon Example

Read Now →
ā˜…

How to Get Hours Difference Between Two Dates in Laravel?

Read Now →
ā˜…

How to Get Month Difference Between Two Dates in Laravel?

Read Now →
ā˜…

How to Find Day Name from Specific Date in PHP?

Read Now →
ā˜…

How to Get Difference Between Two Dates in Laravel?

Read Now →