Solved

Difference in hours between dates

Posted on 2013-01-22
272 Views
I have two different dates and different times, I would like to know how to do the calculation to get the difference in hours between these two dates example:

\$Date1 = "2012-01-01";
\$Date2 = "2012-01-02";

\$hora1 = "20:00:00";
\$hora2 = "21:00:00";

How to calculate the difference between days and hours?
0
Question by:eduardo12fox
• 4

LVL 15

Expert Comment

ID: 38806768
0

LVL 15

Expert Comment

ID: 38806769
0

LVL 15

Expert Comment

ID: 38806798
date_sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

http://php.net/manual/en/datetime.sub.php
0

LVL 15

Accepted Solution

Ess Kay earned 500 total points
ID: 38806801
or use this
http://stackoverflow.com/questions/3371011/how-to-subtract-two-dates-and-times-to-get-difference

<?php
//\$now = new DateTime(); // current date/time
\$now = new DateTime("2010-07-28 01:11:50");
\$ref = new DateTime("2010-07-30 05:56:40");
\$diff = \$now->diff(\$ref);
printf('%d days, %d hours, %d minutes', \$diff->d, \$diff->h, \$diff->i);
prints 2 days, 4 hours, 44 minutes

see http://docs.php.net/datetime.diff

edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
0

Author Closing Comment

ID: 38808159
Very, very thank you!!
0

Featured Post

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.