Page 1 of 1

HELP in Timestamp Difference

Posted: Fri Jun 30, 2006 8:42 am
by mcoelho123
Someone can help me making a script to:

1 - Add 5 hours and 10 minutes to actual date/time (that is the diference for my time and the time of the webserver),
2 - Calculate the diference between 2 dates last login and now,
3 - The final step put the diference time in a variable and echo "Your Last Login time was about 5 hours ago", "Your Last Login time was about 5 minutes ago" and "Your Last Login time was about 5 weeks ago"


My webserver is Windows
Thanks in advance

Posted: Fri Jun 30, 2006 8:58 am
by John Cartwright
Firstly, timestamps are simply seconds since epoch.. so to add a certain time to a timestamp simply add the equivilent in seconds...

1)

Code: Select all

//add 5 hours and 10 minutes
$timestamp += 18600;


2) You should be able to figure out the second one, simply minus the previous timestamp from time() :wink:

3) I would probably throw the difference variable in an if/elseif to get the correct wording output.. something along the lines of

Code: Select all

if ($difference < 60)
{
   $since = $difference;
   $period = 'seconds';
}
elseif ($difference < 3600)
{
   $since = ceil($difference / 60);
   $period = 'minutes';
}
elseif ($difference < 86400)
{
   $since = ceil($difference / 3600);
   $period = 'hours';
}
else
{
   $since = ceil($difference / 86400);
   $period = 'days';
}

echo 'It has been '. $since .' '. $period .' since your last login';

Posted: Fri Jun 30, 2006 9:20 am
by mcoelho123
Ok, i go it
thank u very much :lol: