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
Medium Priority
403 Views
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
Question by:Pedro Chagas
• 2

LVL 61

Expert Comment

ID: 38421129
``````\$t1 = strtotime(\$old_time . ' ' . \$old_hour);
\$t2 = strtotime(\$actual_time . ' ' . \$actual_hour);
\$difference = \$t2 - \$t1;
``````
0

LVL 61

Accepted Solution

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;
``````
0

## Featured Post

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
Course of the Month14 days, 13 hours left to enroll