Created
March 12, 2012 18:06
-
-
Save Eclarian/2023695 to your computer and use it in GitHub Desktop.
PHP DateTime Object Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* EC_DateTime | |
* | |
* By default in the PHP object, the date property is not affected when the | |
* method format() is run. However, it's annoying to have to constantly set | |
* the format you desire to output. Also, you may not have access when the | |
* __toString() method is executed in another function if passed as an object. | |
* and then treated as a string. | |
* | |
* So what this object allows for us to do is to access that formatted date | |
* and also to echo the $date variable that holds our EC_DateTime Object | |
* | |
* @author Joel Kallman | |
*/ | |
class EC_DateTime extends DateTime { | |
/** | |
* Current Format | |
* | |
* Defaults to "Y-m-d H:i:s". | |
* You can set directly: $date->format = 'Y-m-d H:i:s'; | |
* | |
* @access public | |
* @var string | |
*/ | |
public $format = 'Y-m-d H:i:s'; | |
// ---------------------------------------------------------------------------------------- | |
/** | |
* Format | |
* | |
* The change this function introduces is the ability to | |
* get the latest state of the formatted date from the object | |
* | |
* @param string Optional. If not provided it will use the last format OR default | |
* @return string | |
*/ | |
public function format($format = '') | |
{ | |
$this->format = ( ! empty($format)) ? $format: $this->format; | |
return parent::format($this->format); | |
} | |
// ---------------------------------------------------------------------------------------- | |
/** | |
* Copy | |
* | |
* @return object DateTime Object | |
*/ | |
public function copy() | |
{ | |
return new EC_DateTime($this->format('Y-m-d H:i:s'), $this->getTimezone()); | |
} | |
// ---------------------------------------------------------------------------------------- | |
/** | |
* __toString | |
* | |
* Outputs Most Recently Formatted Date | |
*/ | |
public function __toString() | |
{ | |
return $this->format(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment