Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the hour,min between two date time? [duplicate]

Tags:

php

cakephp

I have two date's

$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"

I want to calculate the difference and display it as follows

1 hour 32 minutes

like image 550
alamin8031 Avatar asked Feb 11 '14 04:02

alamin8031


People also ask

How do you calculate hours and minutes between two dates?

Dates and times in Excel are stored as a date number, which is the number of days since 1 January 1900, with the value after the decimal point representing the time of day. To calculate the number of hours between two dates we can simply subtract the two values and multiply by 24.

How do I calculate hours between two times?

To calculate the number of hours between two times:Subtract the starting time from the ending time. Do you have any seconds or minutes after the subtraction? If so: Take seconds, divide them by 60, and add the result to the minutes.

How do I calculate hours between two dates in Excel?

Calculate hours between two times: =TEXT(B2-A2, "h") Return hours and minutes between 2 times: =TEXT(B2-A2, "h:mm") Return hours, minutes and seconds between 2 times: =TEXT(B2-A2, "h:mm:ss")

How do I calculate the number of minutes between two dates?

To get the number of minutes between 2 dates: Get the number of milliseconds between the unix epoch and the Dates. Subtract the milliseconds of the start date from the milliseconds of the end date. Divide the result by the number of milliseconds in a minute - 60 * 1000 .


1 Answers

Simply convert both dates to timestamp if dont want to do it in complex way... Something like this

$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);

$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;

and there you go...

Thank you...

like image 167
Vedant Joshi Avatar answered Sep 30 '22 00:09

Vedant Joshi