Home > Development, PHP > Format a Mysql DateTime Field with Zend_Date

Format a Mysql DateTime Field with Zend_Date

Zend_Date is really time saver class, I use it in almost every project I work, It handles time_zones, and date formatting pretty easy, leaving me more time to focus on other things.

The files that I need to use Zend_Date as a standalone class are:

Date.php and the Date folder from the Zend distribution package, and this is how I use it:

< ?php
set_include_path('./lib' . PATH_SEPARATOR . get_include_path());
include_once 'Zend/Date.php';
$tugId = isset($_GET['tugId']) ? (int) $_GET['tugId'] : false;
 
function formatDate($date) {
    /**
     * List of supported timezones
     * http://php.net/manual/en/timezones.php
     */
    if ($date) {
        $fmtDate = new Zend_Date($date, Zend_Date::ISO_8601);
        $fmtDate->setTimezone('America/Los_Angeles');
        return $fmtDate->toString(Zend_Date::DATETIME);
    } else {
        return '';
    }
}
 
if (is_int($tugId)) {
 $sql = 'SELECT modified_date FROM tuggler WHERE tug_id = ' . $tugId;
 $result = $db->fetchAssoc($sql);
 $response = 'Last Record Update: '.formatDate($row['modified_date']) .'<br />';
} else {
 $response "nothing to return";
}
 
echo $response;

The response from the above would be Jul 9, 2009 11:41:20 AM but I can easily change it using one of the predefined constants, or I can make my own format.

 

Categories: Development, PHP Tags:
  1. No comments yet.
  1. No trackbacks yet.