Jump to content

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');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.