Version: 1.0

Type: Class

Category: Calendars/Dates

License: GNU Library Public License

Description: The elapsed:: class keeps a record of a specific time using a simple text file referred to as a Token. It provides simple tools to check elapsed time.



<?php
/**
 * The elepsed:: class keeps a record of a specific time
 * using a simple text file reffered to as a Token. It 
 * provides simple tools to check elepsed time.
 *
 * Copyright 2007-2008 Adam GRandt <adam@inuwashi.net>
 *
 * This library is provided under LGPL. see http://www.fsf.org/copyleft/lgpl.html.
 *
 */



//***************************************************************************//
//	This Libary may NOT function due to safe mode  .                         //
//	Please make sure to put all relevant files in the safe_mode_include_dir. //
//***************************************************************************//

class ElepsedTimer { 

	

	
/**
 *
 * var
 * 
 * $filenameis the name and pathe of the file that will
 * contain the last timestamp. This path must be writble by
 * the web server! The defult value is "TimerToken" wich
 * you can change at runtime. 
 *
 * $this->$debug: if == 1 shows debug comments
 * 
 */

public $FileName = "TimerToken";
public $debug = 0;


/**
 * 
 * Constructor.
 * 
 */

	function __construct()
	{		
		
			
		if (file_exists($this->FileName)&(is_writable($this->FileName))) 
		{
			if ($this->debug == 1){ echo "The Elepsed token was found and is accessible";}
			return true;
		}
		else 
		{
			if (@touch($this->FileName)) 
			{
				$this ->update(); 
				if ($this->debug == 1){ echo "The Elepsed token was created & updated successfully"; }
			} 
			else 
			{
				if ($this->debug == 1){ echo "Sorry, The Elepsed token named  <b>$this->FileName</b> was not found and could not be created.<br>Please verify the permission settings of your web-root dir.<br>";}
				exit("<h4>Sorry, We have encountered a fatal error in the Elepsed module. INU0001</h4>");
			}
		} 
	}
	


public function update()
	{		
		if (file_put_contents("$this->FileName",time()))
		{
			if ($this->debug == 1){ echo "The Elepsed token was updated successfully";}
		}
		else 
		{
			if ($this->debug == 1){ echo "Sorry, The Elepsed token could not be updated.<br>";}
			exit("<h4>Sorry, We have encountered a fatal error in the Elepsed module. INU0002</h4>");
		}
	}

/**
 *
 * Reaturns the pretty date format of the time stamp in the token.
 * You can send in any date func format you like
 *
 */
	public function show($DateFormat = 'l dS \of F Y h:i:s A')
	{		
		if ($token = file_get_contents("$this->FileName") )
		{
			return date($DateFormat,$token);
		}
		else 
		{
			if ($this->debug == 1){ echo "Sorry, The elepsed token could not be read.<br>";}
			exit("<h4>Sorry, We have encountered a fatal error in the Elepsed module. INU0003</h4>");
		}
	}		
	
	
/**
 *
 * Reaturns the elepsed timesince the token.
 * You can send in "s" for Seconds (def), "m" for Minuets,
 *  "h" for Hours,  * or "d" for Days.
 * Note: You must send the format as a STRING!
 *
 */
	public function elepsed($format = "s")
	{	
	
		if ($token = file_get_contents("$this->FileName") )
		{
				switch ($format) {
					case "s":
						return (number_format(time()-$token,0));
						break;
						
					case "m":
						return (number_format((time()-$token)/60,0));
						break;
							
					case "h":
						return (number_format((time()-$token)/3600,0));
						break;	

					case "d":
						return (number_format((time()-$token)/86400,0));
						break;	
				
					default:
						break;
		}
		}
		else 
		{
			if ($this->debug == 1){ echo "Sorry, The Elepsed token could not be read.<br>";}
			exit("<h4>Sorry, We have encountered a fatal error in the Elepsed module. INU0004</h4>");
		}
		
	}
	
	
}


?>