Monday, June 27, 2011

Monday, June 27, 2011

Here is a class for working with time


<?php
class Date{
//purpose: implents an ECMA-style Date class for PHP
protected $time;
protected $date;

//static functions

public static function parse($date)
{
return strtotime($date);
}

public static function UTC($year, $month, $day)
{
$hours = $minutes = $seconds = 0;
$num_args = func_num_args();
if($num_args>3)
{
$hours=func_get_arg(3);
}
if($num_args>4)
{
$minutes=func_get_arg(4) + ((int)date('Z')*60);
}
if($num_args>5)
{
$seconds = func_get_arg(5);
}
return mktime($hours, $minutes, $seconds, ($month+1), $day, $year);
}

//------------
// constructor

public function __construct()
{
$num_args = func_num_args();
if($num_args > 0)
{
$args = func_get_args();
if(is_array($args[0]))
{
$args = $args[0];
$num_args = count($args);
}
}
if($num_args>1)
{
$seconds = $minutes = $hours = $day = $month = $year = 0;
}
switch($num_args)
{
case 6:
$seconds = $args[5];
case 5:
$minutes = $args[4];
case 4:
$hours = $args[3];
case 3:
$hours = $args[2];
case 2:
$hours = $args[1];
$year = $args[0];
$this->time = mktime($hours, $minutes, $seconds, ($month + 1), $day, $year);
break;
case 1:
if(is_int($args[0]))
{
$this->time = $args[0];
}
elseif(is_string($args[0]))
{
$this->time = strtotime($args[0]);
}
break;
//if no arguments are passed to the constructor, then $time is set to the default
//value returned by mktime() when called without input parameters
//aka the current system date and time
case 0:
$this->time = mktime();
break;
}
$temp = gettimeofday();
$this->offset = (int)$temp["minuteswest"];
} //end construct function

//returns day of the month
public function getDate()
{
return (int)date("j", $this->time);
}

//returns day of the week
public function getDay()
{
return (int)date("w", $this->time);
}

//returns 4 digit year
public function getFullYear()
{
return (int)date("Y", $this->time);
}

//return hours field (0-23)
public function getHours()
{
return (int)date("H", $this->time);
}

//returns mintues field
public function getMinutes()
{
return (int)date("i", $this->time);
}

//returns month
public function getMonth()
{
$temp = (int)date("n", $this->time);
return --$temp;
}

public function getSeconds()
{
return (int)date("n", $this->time);
}

//returns a complete date as elapsed seconds
//since the UNIX epoch
public function getTime()
{
return $this->time;
}

public function getTimezoneOffset()
{
return $this->offset;
}

public function setDate($date)
{
$this->time = mktime(
$this->getHours(),
$this->getMinutes(),
$this->getSeconds(),
$this->getMonth() + 1,
$date,
$this->getFullYear()
);
return $this->time;
}

public function setFullYear($year)
{
$this->time = mktime(
$this->getHours(),
$this->getMinutes(),
$this->getSeconds(),
($this->getMonth()+1),
$this->getDate(),
$year
);
return $this->time;
}

public function setHours($hours)
{
$this->time = mktime($hours,
$this->getMinutes(),
$this->getSeconds(),
$this->getMonth() + 1,
$this->getDate(),
$this->getFullYear()
);
return $this->time;
}

public function setMinutes($minutes)
{
$this->time = mktime(
$this->getHours(),
$minutes,
$this->getSeconds(),
$this->getMonth()+1,
$this->getDate(),
$this->getFullYear()
);
}

public function setMonth($month)
{
$this->time = mktime(
$this->getHours(),
$this->getMinutes(),
$this->getSeconds(),
$month,
$this->getDate(),
$this->getFullYear()
);
return $this->time;
}

public function setSeconds($seconds)
{
$this->time = mktime (
$this->getHours(),
$this->getMinutes(),
$seconds,
$this->getMonth() + 1,
$this->getDate(),
$this->getFullYear()
);
return $this->time;
}

public function toGMTString()
{
return $this->toUTCString();
}

public function toLocaleString()
{
return date('r', $this->time);
}

public function toUTCString()
{
return date("D d M Y H:i:s", ($this->time + ($this->offset * 60))) . " UTC";
}

public function valueOf()
{
return $this->time;
}

} //end class Date

//set day of month

$date = new Date(1309146256); // may 11th 1982
$month = $date->getMonth();
echo $month;
echo "
";
$date->setDate(22);
$newdate = $date->getDate();
echo $newdate;

//-----------
$today = new Date;
printf("

Current date and time: %s

\n", $today->toLocaleString());

echo "

'Get' methods:

";
printf("

Month %d.

\n", $today->getMonth());
printf("

Day of month: %d.

\n", $today->getDate());
printf("

Day of Week: %d.

\n", $today->getDay());
printf("

Year: %d.

\n", $today->getFullYear());
printf("

Hours: %d.

\n", $today->getHours());
printf("

Minutes: %d

\n", $today->getMinutes());


$newdate = "Sat, 4 April 2003 15:15:25 +1000";
$timestamp = Date::parse($newdate);
printf("

Test dateL %s; Date::parse() yields: %d.

\n", $newdate, Date::parse($newdate));
printf("

Using toUTCstring(): %s

", $today->toUTCString());
echo "Now for some 'set' methods...

";

echo "

Let's try advancing the date by one day...:";
$today->setDate($today->getDate() +1);
echo $today->toLocaleString() . "

";

echo "

Now let's try advancing the date by one year...:";
$today->setFullYear($today->getFullYear()+1);
echo $today->toLocaleString() . "

";

echo "

Now we're going to set the month for that date to 0 (January):";
$today->setMonth(0);
echo $today->toLocaleString() . "

\n";
?>



here is another tutorial that extends the above class.


<
class DateExtended extends Date
{
public static function isLeapYear($year)
{
return date('L', mktime(0, 0, 0, 1, 1, $year)) == 1 ? TRUE : FALSE;
}

//class constructor: passes whatever arguments it receives back ot the parent class constructor
public function __construct()
{
parent::__construct(func_get_args());
}

public function toLocaleString($long=FALSE)
{
$output = "";
if($long)
{
$day=$this->getDayFullName();
$date=$this->getOrdinalDate();
$month=$this->getMonthFullName();
$year=$this->getFullYear();
$time=$this->getClockTime(TRUE, TRUE, FALSE);

$output="$day, $date $month $year, $time";
} else {
$output=date('r', $this->getTime());
}
return $output;
}


public function getClockTime($twelve = TRUE, $uppercaseAMPM=TRUE, $includeSeconds=TRUE, $separator=":")
{
$am_pm="";
$hours = $this->getHours();
if($twelve)
{
$am_pm = " " . ($hours >= 12 ? "pm" : "am");
if($uppercaseAMPM)
{
$am_pm = strtoupper($am_pm);
}
if ($hours > 12)
{
$hours -= 12;
}
}
else
{
if($hours < 10)
{
$hours = "0$hours";
}
}
$minutes = $this->getMinutes();
if($minutes<10)
{
$minutes = "0$minutes";
}
$minutes = "$separator$minutes";
$seconds ="";
if($includeSeconds)
{
$seconds = $this->getSeconds();
if($seconds<10)
{
$seconds="0$seconds";
}
$seconds = "$separator$seconds";
}
return "$hours$minutes$seconds$am_pm";
}

public function getDayFullName()
{
return date('l', $this->time);
}

//return 3-letter abbreviation for day of week
//(sun, mon, etc)
public function getDayShortName()
{
return date('D', $this->time);
}

//returns number of days in current month
public function getDaysInMonth()
{
return date('t', $this->time);
}

public function getDifference(Date $date)
{
$val1 = $this->getTime();
$val2 = $date->getTime();
$sec = abs($val2 - $val1);
$units = getdate($sec);

$hours = abs($units["hours"] - (date('Z') / 3600));
$days = $units["mday"];

if($hours > 23)
{
$days++;
$hours %= 24;
}
$output = array();
$output["components"] = array("years" => $units["year"] -1970,
"months"=>--$units["mon"],
"days"=>--$days,
"hours"=>$hours,
"minutes"=>$units["minutes"],
"seconds"=>$units["seconds"]
);

$output["elapsed"] = array("years" => $sec / (365*24*60*60),
"months"=>$sec / (30*24*60*60),
"weeks"=>$sec /(7*24*60*60),
"days"=>$sec / (24*60*60),
"hours"=>$sec / (60*60),
"minutes"=>$sec / 60,
"seconds"=>$sec
);
$output["order"] = $val2 < $val1 ? -1 : 1;
return $output;
}

public function getMonthFullName()
{
return date('F', $this->time);
}

//returns 3-letter abbreviation for month
public function getMonthShortName()
{
return date('M', $this->time);
}

public function getOrdinalDate()
{
return date('jS', $this->time);
}

public function getTimeZoneName()
{
return date('T', $this-time);
}

//returns ISO week number
public function getISOWeek()
{
return (int)date('W', $this->time);
}

//returns true if current date/time is daylight-savings time
public function isDST()
{
return date('I', $this->time)==1 ? TRUE : FALSE;
}

//returns true if day is a weekday (Mon-Fri), FALSE if not
public function isWeekDay()
{
$w = $this->getDay();
return ($w > 0 && $w < 6) ? true : FALSE;
}

//returns ISO representation of date and time
public function toISOString()
{
return date('c', $this->time);
}

} //end class DateExtended

//---------
$x= new DateExtended ();
printf("

Current date and time (long toLocaleString()): %s.</p>\n", $x->toLocaleString(TRUE));
printf("

Current date and time (parent toLocaleString()): %s.</p>\n", $x->toLocaleString());
printf("

Current date and time (ISO format): %s.

\n", $x->toISOString());

printf("

Today is %s (%s).

\n", $x->getDayFullName(),$x->getDayShortName());
printf("

Today is %s %s, %d.

\n", $x->getOrdinalDate(), $x->getMonthFullName(), $x->getFullYear());
for($year=2000;$year<2015;$year++){
printf("%s is %s a leap year.<br/>\n", $year, DateExtended::isLeapYear($year) ? '' : 'not');
echo "<br/>";
}
$past = new DateExtended(1997, 6, 4, 15, 30, 45);
printf("

Past date is %s.</p>\n", $past->toLocaleString());
$diff=$x->getDifference($past);
$components = $diff["components"];
$output = array();
foreach($components as $period=>$length)
{
if($length>0){
$output[]= "$length $period";
}
}
printf("

Difference in dates is: %s.

", implode($output, ', '));
printf("

Difference in dates is: %s years.

", $diff["elapsed"]["years"]);



//set day of month

$date = new Date(1309146256); // may 11th 1982
$month = $date->getMonth();
echo $month;
echo "<br/>";
$date->setDate(22);
$newdate = $date->getDate();
echo $newdate;

No comments: