Jump to content

Calculate hours between to dates.


huxphlux

Recommended Posts

Hello everyone, had a look around on google an such, find some there, but

it didnt help me at all. But hopefully someone here could help me. :)

All help is really appriciated!

 

So, to the "problem" or what i shall call it. :)

 

I got too different dates in a database. Ive fetched them from it and stored it into a variabels. Lets say like this.

$date1 = 2010-07-20 20:00:00

$date2 = 2010-07-21 20:00:00

 

So, these dates is just examples. So what i want to do is to find out how many hours it is between them and store

the hours into a variable. And yes, they are stored like that in the database, so its not unixtime.

 

If you need to know more, please ask. :)

 

Great thanks.

Link to comment
https://forums.phpfreaks.com/topic/208235-calculate-hours-between-to-dates/
Share on other sites

Use DateTime::diff (or it's procedural equivalent date_diff)

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');

And btw, is not that compareing and give you how many days? I would want hours..

 

quote author=Cagecrawler link=topic=304782.msg1441660#msg1441660 date=1279582838]

Use DateTime::diff (or it's procedural equivalent date_diff)

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.