Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Php - Compare data and time and output the difference

Posted on 2012-09-21
2
Medium Priority
?
403 Views
Last Modified: 2012-09-21
Hi E's, I need to know how I compare two times and output th difference between them.
In practice I have two time, compose by the day and the hours:
$old_time = "2012-09-20";
$old_hour = "23:10:57"; //(hour:minutes:seconds)
$actual_time = "2012-09-21";
$actual_hour = "13:17:44"; //(hour:minutes:seconds)
$difference = "days:hours:minutes:seconds"; //difference between old time and actual time

How I get the result of $difference variable?

The best regards, JC
0
Comment
Question by:Pedro Chagas
  • 2
2 Comments
 
LVL 61

Expert Comment

by:Julian Hansen
ID: 38421129
$t1 = strtotime($old_time . ' ' . $old_hour);
$t2 = strtotime($actual_time . ' ' . $actual_hour);
$difference = $t2 - $t1;

Open in new window

0
 
LVL 61

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38421134
Full code to get time string
$old_time = "2012-09-20";
$old_hour = "23:10:57"; //(hour:minutes:seconds)
$actual_time = "2012-09-21";
$actual_hour = "13:17:44"; //(hour:minutes:seconds)

$t1 = strtotime($old_time . ' ' . $old_hour);
$t2 = strtotime($actual_time . ' ' . $actual_hour);
$difference = $t2 - $t1;

$days = intval($difference / 86400);
$hours = intval(($difference % 86400) / 3600);
$min = intval(($difference % 3600) / 60);
$sec = $difference % 60;
$diffstr = sprintf("%02d:%02d:%02d:%02d", $days, $hours, $min, $sec);
echo $diffstr;

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses

578 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