note 109035 modified in function.date by danbrown

From: Date: Thu, 14 Jun 2012 17:39:57 +0000
Subject: note 109035 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188615@lists.php.net to get a copy of this message
Here is a cool Date class to implement the date function:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;/*&#13;&#10; * @author&#9;Gchats&#13;&#10; *&#13;&#10; * Date class&#13;&#10; */&#13;&#10;class Date&#13;&#10;{&#9;&#13;&#10;&#9;private $shortDateFormat = &#34;F j, Y&#34;;&#13;&#10;&#9;private $longDateFormat = &#34;F j, Y, g:i a&#34;;&#13;&#10;&#9;private $timestamp = 0;&#13;&#10;&#9;&#13;&#10;&#9;/**&#13;&#10;&#9;* Default constructor&#13;&#10;&#9;*&#13;&#10;&#9;* @param&#9;integer&#9;&#9;$timestamp&#9;unix time stamp&#13;&#10;&#9;*/&#13;&#10;&#9;function __construct($timestamp = 0)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;$this-&#62;timestamp = $timestamp;&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;/**&#13;&#10;&#9;* Returns the given timestamp in the constructor&#13;&#10;&#9;*&#13;&#10;&#9;* @return&#9;integer&#9;&#9;time stamp&#13;&#10;&#9;*/&#13;&#10;&#9;public function getTime()&#13;&#10;&#9;{&#13;&#10;&#9;&#9;return (int) $this-&#62;timestamp;&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;/*&#13;&#10;&#9; * Returns long formatted date of the given timestamp&#13;&#10;&#9; *&#13;&#10;&#9; * @access public&#13;&#10;&#9; * @return &#9;string&#9;Long formatted date&#13;&#10;&#9; */&#13;&#10;&#9;public function long()&#13;&#10;&#9;{&#13;&#10;&#9;&#9;if ( $this-&#62;timestamp &#62; 0 )&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;return date ( $this-&#62;longDateFormat , $this-&#62;timestamp );&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;else&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;return &#34;&#34;;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;/*&#13;&#10;&#9; * Returns short formatted date of the given timestamp&#13;&#10;&#9; *&#13;&#10;&#9; * @access public&#13;&#10;&#9; * @return &#9;string&#9;Short formatted date&#13;&#10;&#9; */&#9;&#13;&#10;&#9;public function short()&#13;&#10;&#9;{&#13;&#10;&#9;&#9;if ( $this-&#62;timestamp &#62; 0 )&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;return date ( $this-&#62;shortDateFormat , $this-&#62;timestamp );&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;else&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;return &#34;&#34;;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;public function __toString()&#13;&#10;&#9;{&#13;&#10;&#9;&#9;return $this-&#62;timestamp;&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;}&#13;&#10;?&#62; --was-- Here is a cool Date class to implement the date function: <?php /* * @author Gchats http://www.gchats.com * * Date class */ class Date { private $shortDateFormat = "F j, Y"; private $longDateFormat = "F j, Y, g:i a"; private $timestamp = 0; /** * Default constructor * * @param integer $timestamp unix time stamp */ function __construct($timestamp = 0) { $this->timestamp = $timestamp; } /** * Returns the given timestamp in the constructor * * @return integer time stamp */ public function getTime() { return (int) $this->timestamp; } /* * Returns long formatted date of the given timestamp * * @access public * @return string Long formatted date */ public function long() { if ( $this->timestamp > 0 ) { return date ( $this->longDateFormat , $this->timestamp ); } else { return ""; } } /* * Returns short formatted date of the given timestamp * * @access public * @return string Short formatted date */ public function short() { if ( $this->timestamp > 0 ) { return date ( $this->shortDateFormat , $this->timestamp ); } else { return ""; } } public function __toString() { return $this->timestamp; } } ?> http://php.net/manual/en/function.date.php

« previous php.notes (#188615) next »