Solved

Difference in hours between dates

Posted on 2013-01-22
5
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

628 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