note 21798 added to function.strftime
| From: | gregory at justretail dot com | Date: | Mon, 27 May 2002 17:09:39 +0000 |
| Subject: | note 21798 added to function.strftime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31076@lists.php.net to get a copy of this message | ||
Using a class to format a date.
This example shows how to format a date using a timestamp. You can obtain the timestamp through
your MySQL Select statement, e.g, SELECT UNIX_TIMESTAMP(date_field) as my_timestamp FROM table, or
by using strtotime, e.g., $my_timestamp = strtotime (date_field).
// This class expects a date array value and returns a formatted date. You can add additional
methods to return different date formats--obviously!
<?php
class DateMake {
var $month;
var $day;
var $year;
function DateMake($ts="") {
$date_time_array = $ts;
$this->month = $date_time_array["mon"];
$this->day = $date_time_array["mday"];
$this->year = $date_time_array["year"];
return ($this->month."/".$this->day."/".$this->year);
}
function Month() {
return $this->month;
}
function Day() {
return $this->day;
}
function Year() {
return $this->year;
}
}
?>
// Add the "require_once" reference
to each php page which needs to use this class.
require_once("class.DateMake.php");
// instantiate the class
$oDateMake = new DateMake();
// call the method. The timestamp is passed to getdate(), (you can also pass an empty getdate() to
the class to return the current date).
$myDate = $oDateMake->DateMake(getdate($myresult['my_timestamp']));
You can modify this class or create your own date formatting class; whichever you choose, the
important thing to note about classes is that they can replace in-line data formatting code in your
php pages with re-usable polymorphic code.
--
http://www.php.net/manual/en/function.strftime.php
http://master.php.net/manage/user-notes.php?action=edit+21798
http://master.php.net/manage/user-notes.php?action=delete+21798
http://master.php.net/manage/user-notes.php?action=reject+21798