Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Difference in hours between dates

Posted on 2013-01-22
5
Medium Priority
?
287 Views
Last Modified: 2013-01-22
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
Comment
Question by:eduardo12fox
  • 4
5 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38806768
use date_add
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38806769
0
 
LVL 15

Expert Comment

by:Ess Kay
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

by:
Ess Kay earned 2000 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

by:eduardo12fox
ID: 38808159
Very, very thank you!!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question