Skip to content

Instantly share code, notes, and snippets.

@jpfuentes2
Created April 13, 2010 21:29
Show Gist options
  • Save jpfuentes2/365107 to your computer and use it in GitHub Desktop.
Save jpfuentes2/365107 to your computer and use it in GitHub Desktop.
<?php
class DateTime extends \DateTime
{
private static $DATE_NAMES = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
private static $FORMATS = array('ymd' => 'Y-m-d', 'jenark' => 'm/d/y', 'mysql' => 'Y-m-d');
private static $DEFAULT_FORMAT = 'ymd';
public function __call($method, $args)
{
if (substr($method, 0, 2) === "is" && in_array(substr($method, 2), self::$DATE_NAMES))
{
if ($this->dayName() == substr($method, 2))
return true;
else
return false;
}
}
public function dayName()
{
return date('l', $this->getTimestamp());
}
public function format($format=null)
{
if (($f = self::getFormat($format)))
return parent::format($f);
elseif (!is_null($format))
return parent::format($format);
return parent::format(self::getDefaultFormat());
}
static public function getFormat($format=null)
{
if (!is_null($format) && array_key_exists($format, self::$FORMATS))
return self::$FORMATS[$format];
return null;
}
static public function getDefaultFormatName()
{
return self::$DEFAULT_FORMAT;
}
static public function getDefaultFormat()
{
return self::getFormat(self::getDefaultFormatName());
}
static public function setDefaultFormat($format)
{
self::$DEFAULT_FORMAT = $format;
}
static public function getLastMonday()
{
return new self('Last Monday');
}
public function __toString()
{
return $this->format();
}
static public function create($time = "now", DateTimeZone $timezone = NULL)
{
if ($timezone != NULL)
return new self($time, $timezone);
else
return new self($time);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment