Date Time Php

//
// class.datetime_functions.php
//
// Includes a number of functions that are date/time related.
//
//
class datetime_functions
{
// constructor
function datetime_functions()
{
$this->_class_name = "Date/Time Functions Class";
$this->_class_version = "1.0.0";
$this->_class_author = "xxx";
}
// returns a nicely formatted date string for a SQL date format
function sqldate_to_string($t="",$short=0)
{
if ($t=="") return "";
if (!$short) $months = array(
"January","Februrary","March","April","May","June","July",
"August","September","October","November","December"
);
else $months = array(
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
);
if (ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})",$t,$args))
return sprintf("%s %d, %s",$months[$args[2]-1],$args[3],$args[1]);
else if (ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$t,$args))
return sprintf("%s %d, %s",$months[$args[2]-1],$args[3],$args[1]);
else return $t;
}
// a function to convert number of seconds (eg; 96172) to a readable format
// such as: 1 day, 2 hours, 42 mins, and 52 secs
// takes unix timestamp as input
function time_to_string($t=0)
{
if (!$t) return "no time at all";
if ($t < 0) {
$neg = 1;
$t = 0 - $t;
}
$days = $t / 86400;
$days = floor($days);
$hrs = ($t / 3600) % 24;
$mins = ($t / 60) % 60;
$secs = $t % 60;
$timestring = "";
if ($neg) $timestring .= "negative ";
if ($days) {
$timestring .= "$days day" . ($days==1?"":"s");
if ($hrs || $mins || $secs) $timestring .= ", ";
}
if ($hrs) {
$timestring .= "$hrs hour" . ($hrs==1?"":"s");
if ($mins && $secs) $timestring .= ", ";
if (($mins && !$secs) || (!$mins && $secs)) $timestring .= " and ";
}
if ($mins) {
$timestring .= "$mins min" . ($mins==1?"":"s");
if ($mins && $secs) $timestring .= ", ";
if ($secs) $timestring .= " and ";
}
if ($secs) $timestring .= "$secs sec" . ($secs==1?"":"s");
return $timestring;
}
// a function to return a string of when the file was last updated in a slightly
// fuzzy way. Takes a unix timestamp as input.
function FuzzyTime($time)
{
// sod = start of day :)
$now = time();
$sod = mktime(0,0,0,date("m",$time),date("d",$time),date("Y",$time));
$sod_now = mktime(0,0,0,date("m",$now),date("d",$now),date("Y",$now));
if ($sod_now == $sod) return "today at " . date("g:ia",$time); // check 'today'
if (($sod_now-$sod) <= 86400) return "yesterday at " . date("g:ia",$time); // check 'yesterday'
if (($sod_now-$sod) <= (ONE_DAY*5)) return date("l \a\\t g:ia",$time); // give a day name if within the last 5 days
if (date("Y",$now) == date("Y",$time)) return date("M j \a\\t g:ia",$time); // miss off the year if it's this year
return date("M j, Y \a\\t g:ia",$time); // return the date as normal
}
} // end class
?>
And just in case you want an example script for the above,
example1.php
/*
Original example script for class.datetime_functions.php
Lets begin
Brief description and purpose of this file
To give you a clear cut view on how the class can become useful.
There aew three functions in the class:
1. sqldate_to_string();
2. time_to_string();
3. Fuzzy Time();
The descriptions for the these functions() can be found in the class
I'm just gonna give you an idea on how to use it.
O.K lets begin
*/
include('class.datetime_functions.php');
$functions = new datetime_functions;
$formated_t = $functions -> sqldate_to_string("2004-06-12 17:38:14");
echo('This is to convert the date from sql to a formatted string.
2004-06-12 17:38:14 - becomes - ' . $functions -> sqldate_to_string("2004-06-12 17:38:14"));
echo('

');
echo('This is to convert seconds to a formatted date string.
96172 - becomes - ' . $functions -> time_to_string(96172));
echo('

');
echo('This is to convert a Timestanp to a formatted date string.
20040612173814 - becomes - ' . $functions -> FuzzyTime(20040612173814));
echo('
');
?>