18

I have to calculate date time difference, how to do that in PHP? I need exact hours, mins and secs. Anybody have the scripts for that?

0

6 Answers 6

33

Use the diff() method of PHP's DateTime class like this:-

$lastWeek = new DateTime('last thursday'); $now = new DateTime(); var_dump($now->diff($lastWeek, true)); 

This will give you a DateInterval object:-

object(DateInterval)[48] public 'y' => int 0 public 'm' => int 0 public 'd' => int 2 public 'h' => int 11 public 'i' => int 34 public 's' => int 41 public 'invert' => int 0 public 'days' => int 2 

Retrieving the values you want from that is trivial.

Sign up to request clarification or add additional context in comments.

Comments

10

Check this.. This should work

<?php function timeDiff($firstTime,$lastTime) { // convert to unix timestamps $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); // perform subtraction to get the difference (in seconds) between times $timeDiff=$lastTime-$firstTime; // return the difference return $timeDiff; } //Usage : echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32"); ?> 

4 Comments

Hi vimard thanks for your valuable replay. Actually I want to display the oldness of files like in facebook or orkut. they shows time, days, week, year based display.
so this solves your problem rite...
@Vimard he wanted (4 years ago) to see difference in days, weeks, years not seconds.
i don't understand, it give me 07:00:00 even both number is same ($firstTime and $lastTime)
10

This should work, just replace the times in the time diff with the time the task started and the current time or the time the task had ended. For a continuous counter the start time would be stored in a database, or for elapsed time for a task the end time would as well

function timeDiff($firstTime,$lastTime){ // convert to unix timestamps $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); // perform subtraction to get the difference (in seconds) between times $timeDiff=$lastTime-$firstTime; // return the difference return $timeDiff; } //Usage : $difference = timeDiff("2002-03-16 10:00:00",date("Y-m-d H:i:s")); $years = abs(floor($difference / 31536000)); $days = abs(floor(($difference-($years * 31536000))/86400)); $hours = abs(floor(($difference-($years * 31536000)-($days * 86400))/3600)); $mins = abs(floor(($difference-($years * 31536000)-($days * 86400)-($hours * 3600))/60));#floor($difference / 60); echo "<p>Time Passed: " . $years . " Years, " . $days . " Days, " . $hours . " Hours, " . $mins . " Minutes.</p>"; 

Comments

3

For something like that use the built int time() function.

  • Store the value of time(); something like 1274467343 which is the number of seconds since
  • When ever needed retrieve the value and assign it to $erlierTime.
  • Assign time() again to the latter stop time you would want, lets just call it $latterTime

So now you have something like $erlierTime and $latterTime.

Now just do $latterTime - $erlierTime to get the difference in seconds and then do your divisions to get numb of minutes passed, num of hours passed etc.


In order for me or any one to give you a complete script you would need to tell us what your environment is like and what are you working with mysql, sqlite etc... also would need to know how that timer is triggered.

1 Comment

in my case, I retrieve datetime from database to lattertime, convert it with strtotime(), keep now with time()… and make diff < 3600 (1 hour).
0

The best solution would be to use your custom code, there are many built-in functions in php which can let you do it easily. Date, strtotime and time.

  1. First convert any your dates from "Y-m-d H:i:s" format to Linux time-stamp using strtotime function.
  2. Make your calculations and use time to get current time-stamp if applicable.
  3. Convert you calculated time-stamp to dates using date function.

Comments

0

You have to convert your start datetime and endtime through strtotime.

after that substract the starttime from the end time.

after that pass that difference to date or time format that you needed...

so you will get the exact difference between two date.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.