HELP in Timestamp Difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

HELP in Timestamp Difference

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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';
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post by mcoelho123 »

Ok, i go it
thank u very much :lol:
Post Reply