开发者

How to calculate time difference in PHP? [duplicate]

This question already has answers here: How to calculate the difference between two dates using PHP? (35 answers) 开发者_如何学运维 How to get time difference in minutes in PHP (21 answers) Closed 1 year ago.

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?


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.


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>";


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");

?> 


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

  • Store the value of time(); something like 1274467343 which is the number of seconds scince
  • When ever needed retrive 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.

No 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜