Solved

Difference in hours between dates

Posted on 2013-01-22
5
279 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 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

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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