note 21798 deleted from function.strftime by derick
| From: | derick@php.net | Date: | Sun, 14 Aug 2005 12:02:10 +0000 |
| Subject: | note 21798 deleted from function.strftime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93614@lists.php.net to get a copy of this message | ||
Note Submitter: gregory at justretail dot com
----
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.