Jump to content
MakeWebGames

Timestamps - Tutorial


Dayo

Recommended Posts

Timestamps - Tutorial

Talking on IRC today someone was saying how they use crons insted of using timestamps, in my mind timestamps are much better then a cron (when used propely) here are a few tips on how to use timestamps, i have made a few functions to help using timestamps more easier.

Functions

// Timestamp Tutorial
// Made By Dayo
// Functions by: Dayo

class ts{

// This function comes in handy when you come to use timestamps.
function secs($day=0, $hours=0, $min=0, $sec=0) {
$total = ($day*24*60*60)+($hours*60*60)+($min*60)+$sec;
return $total;
}

function tstotime($var) {
// There are 60 seconds in a minute and 3600 seconds in a hour as well as 86400 seconds in a day
$return=array();
$return['d']=floor($var/86400);
$var=$var-($return['d']*86400);

$return['h']=floor($var/3600);
$var=$var-($return['h']*3600);

$return['m']=floor($var/60);
$var=$var-($return['m']*60);

$return['s']=$var;

return $return;
}

// This function will check your timestamps and will return TRUE or FALSE if it is TRUE prosess your info if not show an error.
function check($check) {
if (time() >= $check){
return TRUE;
} else {
return FALSE;
}
}

// this will tell you when your timestamp was last run  $type must either be D, H, M, S (for days, hours, mins, seconds).
function lastrun($check, $type) {
$diff=time()-$check;
if ($type=='D') { return floor($diff/86400); } 
else if ($type=='H') {return floor($diff/3600);} 
else if ($type=='M') {return floor($diff/60);} 
else if ($type=='S') {return $diff;} 
}


}
$ts=new ts;

How to use

$ts->secs($days, $hours, $mins, $secs);

This just converts the specified ammount of days/hours/mins/seconds to secconds BUT you have to specify each one so if you wanted 1 hour you cant do $ts->secs('1') you have to do $ts->secs('', '1', '', '')

$ts->tstotime($var);

Cant beleive i didnt add this, all this does is divides a timestamp up into days, hours, minutes and seconds. You use it like $ts->tstotime(90061) and that would return an array with the values 'd'=>1, 'h'=>1, 'm'=>1, 's'=>1

$ts->check($check);

This checks if your date specified is before the current date if it is it will return true if not it will be false

$ts->lastrun($check, $type);

This function will return how many days, hours, mins or seconds since the specified date was last run. $type must be either D, H, M or S. also since this rounds the value down there will be a remainder of seconds/mins/hours you may want to find that out, ill edit the function later to return the remainder too

Examples

First of all we will be using time() when echo'ed it will produce someting like 1267039058 (i think i read somewhere this is the amount of seconds since 01/01/1970: 00:00:00).

Now your probley wondering, how am i spose to use this well there is a number of ways you can use it, lets say you have a user you want to put in jail for 5 mins you can use the folowing code

$jtime=time()+(5*60);
mysql_query("UPDATE `users` SET `jail`={$jtime} WHERE id={$id}");


//code to check if user is in jail
if($ts->check($user['jail'])) {
// not in jail
} else {
// in jail
}

//code to check if user is in jail inverted else function by using a `!`
if(!$ts->check($user['jail'])) {
// in jail
} else {
// not in jail
}
Edited by Dayo
Link to comment
Share on other sites

Yup timestamps are alot better, especially for the users point of view.

I've just used them for an auction system that i'll be posting in a little while. :thumbsup:

You could also use them on mail/shoutbox scripts to prevent spam.

Example:

if(time() - $Data['TimeStampInDB'] < 60) { echo 'Nooooo wait 1 min'; }

Link to comment
Share on other sites

Yup timestamps are alot better, especially for the users point of view.

I've just used them for an auction system that i'll be posting in a little while. :thumbsup:

You could also use them on mail/shoutbox scripts to prevent spam.

Example:

if(time() - $Data['TimeStampInDB'] < 60) { echo 'Nooooo wait 1 min'; }

Time stamp in database? There is no need, store it in a session or cookie :)

Link to comment
Share on other sites

I feel that the DB is easier to manage & with it being mail/shoutbox scripts, I would of thought that they would have been used to show the time anyway lol.

If it's in a session all they have to do is logout, signin and do it again & a script to do that isn't hard.

As for cookies, I don't know.

EDIT: Just realised this wasn't in the MC section lol, yes Lazy - Now your right :L if there's no 'logout' system.

Link to comment
Share on other sites

Not all cron jobs should be cron jobs, so the below may not apply to every situation.

If you're using timestamps to replace crons (cron jobs that really should be run as cron jobs), it will probably be best to use a separate table in a database just for this purpose, to keep track of the jobs that have already run so you don't run it twice. Even if you compare by time, it is possible that many people are online at exactly the same time, in which case your cron might run several times at once. Using a table in the database can help keep track of what has already been run.

Link to comment
Share on other sites

  • 1 year later...

UPDATE

just added this function to show how many days, hours, minutes and seconds there are in a timestamp

$ts->tstotime($var);

Cant beleive i didnt add this, all this does is divides a timestamp up into days, hours, minutes and seconds. You use it like $ts->tstotime(90061) and that would return an array with the values 'd'=>1, 'h'=>1, 'm'=>1, 's'=>1

Link to comment
Share on other sites

  • 12 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...