Solved

Difference in hours between dates

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now