1
cesser
Displaying the difference in time between two dates...?
  • 2004/12/12 16:05

  • cesser

  • Just popping in

  • Posts: 26

  • Since: 2004/3/21


Hi all

I was wondering how I would go about extrapolating the actual day and minutes differences between two dates? I am currently using the last_login variable in a block which I am only displaying as an absolute date (ie 2004/12/2).

How could I expand that to display last logged in "2 days, 47 minutes ago..."? I don't know what the variable is for current time, which I'm sure would be needed to figure out the difference. Assuming I know that, how can days and minutes be returned?

Thanks in advance for any help.

2
Dave_L
Re: Displaying the difference in time between two dates...?
  • 2004/12/12 16:31

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


If $last_login is a Unix timestamp (seconds since 1970-01-01 00:00:00 GMT):

$seconds_ago = time() - $last_login;
$days_ago = floor($seconds_ago / (24 * 3600));
$rmdr = $seconds_ago % (24 * 3600);
$hours_ago = floor($rmdr / 3600);
$rmdr = $rmdr % 3600;
$minutes_ago = floor($rmdr / 60);

echo "Last login was $days_ago days, $hours_ago hours, $minutes_ago minutes ago.\n";

3
ackbarr
Re: Displaying the difference in time between two dates...?

we had a similar problem when developing xhelp - our helpdesk application. We created a custom function to take the difference between two times and break it down into units:

The function is used like this:
le="color: #000000"><?php //Assumes that original time is stored in $time2 //Get Current TimeStamp $time1 = time(); //Get Number of elapsed seconds $elapsed = $time1 - $time2; $elapsed = xhelpGetElaspedTime($elapsed); //Now $elapsed is an array: // $elasped['years'] contains the number of years // $elapsed['weeks'] contains the number of weeks // $elapsed['days'] contains the number of days // $elapsed['hours'] contains the number of hours // $elapsed['minutes'] contains the number of minutes // $elapsed['seconds'] contains the number of seconds


And the function is defined like so:
le="color: #000000"><?php function xhelpGetElapsedTime($time) { //Define the units of measure $units = array('years' => (365*60*60*24) /*Value of Unit expressed in seconds*/, 'weeks' => (7*60*60*24), 'days' => (60*60*24), 'hours' => (60*60), 'minutes' => 60, 'seconds' => 1); $local_time = $time; $elapsed_time = array(); //Calculate the total for each unit measure foreach($units as $key=>$single_unit) { $elapsed_time[$key] = floor($local_time / $single_unit); $local_time -= ($elapsed_time[$key] * $single_unit); } return $elapsed_time; }

4
ackbarr
Re: Displaying the difference in time between two dates...?

should you decide to utilize this function, please change its name so that it is not prefixed with 'xhelp' - that way if you install xhelp later there is no chance for a conflict.

Login

Donat-O-Meter

Stats
Goal: $15.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $15.00
Make donations with PayPal!

Latest GitHub Commits