Skip to content

Instantly share code, notes, and snippets.

@dfreerksen
Created January 26, 2013 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dfreerksen/4644962 to your computer and use it in GitHub Desktop.
Save dfreerksen/4644962 to your computer and use it in GitHub Desktop.
PHP Date
<?php
/**
* Date class
*/
class Date {
/**
* Fuzzy date strings
*
* @var array
*/
protected $_time_formats = array(
array(60, 'just now'),
array(90, '1 minute'),
array(3600, 'minutes', 60),
array(5400, '1 hour'),
array(86400, 'hours', 3600),
array(129600, '1 day'),
array(604800, 'days', 86400),
array(907200, '1 week'),
array(2628000, 'weeks', 604800),
array(3942000, '1 month'),
array(31536000, 'months', 2628000),
array(47304000, '1 year'),
array(3153600000, 'years', 31536000)
);
/**
* Timestamp
*
* @var DateTime
*/
protected $_timestamp = null;
/**
* Use current timestamp
*
* @return Date
*/
static public function now() {
return self::with('now');
}
/**
* Specified time
*
* @param int|string $date
* @return Date
*/
static public function with($date) {
return new self($date);
}
/**
* Constructor
*
* @param int|string $date
*/
public function __construct($date) {
// If the timestamp is not valid, set the timestamp to now
if (($this->_timestamp = strtotime($date)) === false) {
$this->_timestamp = strtotime('now');
}
}
/**
* Pretty formatted date. Yes, this is an actual method. Deal with it!
*
* @return string
*/
public function getPrettyDate() {
return $this->getFormated('F j, Y, g:i A');
}
/**
* Timezone string. Or timezone abbreviation
*
* @param bool $abbrev
* @return string
*/
public function getTimezone($abbrev = false) {
$abbrev = ($abbrev) ? 'T' : 'e';
return $this->getFormated($abbrev);
}
/**
* Timezone offset in seconds
*
* @return string
*/
public function getTimezoneOffset() {
return $this->getFormated('z');
}
/**
* Difference to Greenwich time (GMT)
*
* @param bool $colon
* @return string
*/
public function getGmtDiff($colon = false) {
$colon = ($colon) ? 'P' : 'O';
return $this->getFormated($colon);
}
/**
* Current timestamp
*
* @return int
*/
public function getTimestamp() {
return (int) $this->getFormated('U');
}
/**
* Day
*
* @param bool $text
* @param bool $full
* @return string
*/
public function getDay($text = false, $full = true) {
if ($text) {
return $this->getDayText($full);
}
return $this->getDayNum($full);
}
/**
* Day of month. Optionally with leading 0
*
* @param bool $leading
* @return string
*/
public function getDayNum($leading = false) {
$leading = ($leading) ? 'd' : 'j';
return $this->getFormated($leading);
}
/**
* A textual representation of a day
*
* @param bool $full
* @return string
*/
public function getDayText($full = true) {
$full = ($full) ? 'l' : 'D';
return $this->getFormated($full);
}
/**
* English ordinal suffix for the day of the month, 2 characters
*
* @return string
*/
public function getOrdinal() {
return $this->getFormated('S');
}
/**
* Numeric representation of the day of the week
*
* @param bool $iso8601
* @return string
*/
public function getDayOfWeek($iso8601 = false) {
$iso8601 = ($iso8601) ? 'N' : 'w';
return $this->getFormated($iso8601);
}
/**
* The day of the year (starting from 0)
*
* @return string
*/
public function getDayOfYear() {
return $this->getFormated('z');
}
/**
* Number of days in the given month
*
* @return string
*/
public function getDaysInMonth() {
return $this->getFormated('t');
}
/**
* Month
*
* @param bool $text
* @param bool $full
* @return string
*/
public function getMonth($text = false, $full = true) {
if ($text) {
return $this->getMonthText($full);
}
return $this->getMonthNum($full);
}
/**
* Numeric representation of a month
*
* @param bool $leading
* @return string
*/
public function getMonthNum($leading = true) {
$leading = ($leading) ? 'm' : 'n';
return $this->getFormated($leading);
}
/**
* A textual representation of a month
*
* @param bool $abbrev
* @return string
*/
public function getMonthText($abbrev = true) {
$abbrev = ($abbrev) ? 'F' : 'M';
return $this->getFormated($abbrev);
}
/**
* ISO-8601 week number of year, weeks starting on Monday
*
* @return string
*/
public function getWeekOfYear() {
return $this->getFormated('W');
}
/**
* Year representation
*
* @param bool $full
* @return string
*/
public function getYear($full = true) {
$full = ($full) ? 'Y' : 'y';
return $this->getFormated($full);
}
/**
* Whether it's a leap year
*
* @return bool
*/
public function getLeapYear() {
return (bool) $this->getFormated('L');
}
/**
* 12 or 24-hour format of an hour with or without leading zeros
*
* @param bool $leading
* @param bool $military
* @return string
*/
public function getHour($leading = true, $military = true) {
if ($military) {
return $this->getHour12($leading);
}
return $this->getHour24($leading);
}
/**
* 12-hour format of an hour with or without leading zeros
*
* @param bool $leading
* @return string
*/
public function getHour12($leading = true) {
$format = ($leading) ? 'h' : 'g';
return $this->getFormated($format);
}
/**
* 24-hour format of an hour with or without leading zeros
*
* @param bool $leading
* @return string
*/
public function getHour24($leading = true) {
$format = ($leading) ? 'H' : 'G';
return $this->getFormated($format);
}
/**
* Minutes with leading zeros
*
* @return string
*/
public function getMinutes() {
return $this->getFormated('i');
}
/**
* Seconds with leading zeros
*
* @return string
*/
public function getSeconds() {
return $this->getFormated('s');
}
/**
* Microseconds
*
* @return string
*/
public function getMicroseconds() {
return $this->getFormated('u');
}
/**
* Ante meridiem or Post meridiem
*
* @param bool $upper
* @return string
*/
public function getMeridiem($upper = true) {
$upper = ($upper) ? 'A' : 'a';
return $this->getFormated($upper);
}
/**
* Whether or not the date is in daylight saving time
*
* @return bool
*/
public function getDaylightSaving() {
return (bool) $this->getFormated('I');
}
/**
* ISO 8601 date (eg. 2004-02-12T15:19:21+00:00)
*
* @return string
*/
public function getIso8601() {
return $this->getFormated('c');
}
/**
* RFC 2822 formatted date (eg. Thu, 21 Dec 2000 16:01:07 +0200)
*
* @return string
*/
public function getRfc2822() {
return $this->getFormated('r');
}
/**
* Find the age in years
*
* @return int
*/
public function getAge() {
// Current
$now = self::now();
// Differences
$year_diff = $now->getYear() - $this->getYear();
$month_diff = $now->getMonthNum() - $this->getMonthNum();
$day_diff = $now->getDayNum() - $this->getDayNum();
if ($day_diff < 0 || $month_diff < 0) {
$year_diff --;
}
return $year_diff;
}
/**
* Find the relative date
*
* @return string
*/
public function getFuzzy() {
// Difference between now and the passed date
$diff = time() - $this->_timestamp;
$val = '';
// Waaaayyyy to long ago (earlier than 1970)
if ($this->_timestamp <= 0) {
// A long time ago... in a galaxy far, far away
$val = 'a long time ago';
}
// Future date
else if ($diff < 0) {
$val = 'in the future';
}
// Past date
else {
// Loop through each format measurement
foreach ($this->_time_formats as $format) {
// if the difference from now and passed time is less than first option in format measurement
if ($diff < $format[0]) {
// If the format array item has no calculation value
if (count($format) == 2) {
$val = $format[1].($format[0] === 60 ? '' : ' ago');
break;
}
// Divide difference by format item value to get number of units
else {
$val = ceil($diff / $format[2]).' '.$format[1].' ago';
break;
}
}
}
}
return $val;
}
/**
* Generic date format. See http://php.net/manual/en/function.date.php
*
* @param string $format
* @return mixed
*/
public function getFormated($format = 'F j, Y, g:i A T') {
return date($format, $this->_timestamp);
}
}
<?php
$date = Date::now();
$date = Date::with(1358755200);
$date = Date::with('November 24, 1979 06:15:18');
$date = Date::with('now');
$date = Date::with('10 September 2000');
$date = Date::with('+1 day');
$date = Date::with('+1 week');
$date = Date::with('+1 week 2 days 4 hours 2 seconds+1 day');
$date = Date::with('next Thursday');
$date = Date::with('last Monday');
var_dump( $date->getPrettyDate() );
var_dump( $date->getTimezone() );
var_dump( $date->getTimezone(true) );
var_dump( $date->getTimezoneOffset() );
var_dump( $date->getGmtDiff() );
var_dump( $date->getGmtDiff(true) );
var_dump( $date->getTimestamp() );
var_dump( $date->getDay() );
var_dump( $date->getDay(true) );
var_dump( $date->getDay(false, false) );
var_dump( $date->getDay(true, false) );
var_dump( $date->getDayNum() );
var_dump( $date->getDayNum(true) );
var_dump( $date->getDayText() );
var_dump( $date->getDayText(false) );
var_dump( $date->getOrdinal() );
var_dump( $date->getDayOfWeek() );
var_dump( $date->getDayOfWeek(true) );
var_dump( $date->getDayOfYear() );
var_dump( $date->getDaysInMonth() );
var_dump( $date->getMonth() );
var_dump( $date->getMonth(true) );
var_dump( $date->getMonth(false, false) );
var_dump( $date->getMonth(true, false) );
var_dump( $date->getMonthNum() );
var_dump( $date->getMonthNum(false) );
var_dump( $date->getMonthText() );
var_dump( $date->getMonthText(false) );
var_dump( $date->getWeekOfYear() );
var_dump( $date->getYear() );
var_dump( $date->getYear(false) );
var_dump( $date->getLeapYear() );
var_dump( $date->getHour() );
var_dump( $date->getHour(false) );
var_dump( $date->getHour(true, false) );
var_dump( $date->getHour(false, false) );
var_dump( $date->getHour12() );
var_dump( $date->getHour12(false) );
var_dump( $date->getHour24() );
var_dump( $date->getHour24(false) );
var_dump( $date->getMinutes() );
var_dump( $date->getSeconds() );
var_dump( $date->getMicroseconds() );
var_dump( $date->getMeridiem() );
var_dump( $date->getMeridiem(false) );
var_dump( $date->getDaylightSaving() );
var_dump( $date->getIso8601() );
var_dump( $date->getRfc2822() );
var_dump( $date->getAge() );
var_dump( $date->getFuzzy() );
var_dump( $date->getFormated() );
var_dump( $date->getFormated('D M j, Y @ g:i a') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment