Bernhard's Blog
12 Jan
Working with Zend_Date is funny but there is one thing i need and its not integrated in Zend_Date until now. So ive written my own extension to Zend_Date to handle the display of relative dates in my output.
Here are some examples of relative Dates which could be generated by this Script:
Unterneath, you can see my class. Feel free to use it in your own projects.
/*** My Relative Date extension to Zend Frameworks Zend_Date Class** @author Bernhard Rosenberger* @copyright Copyright (c) 2009 Bernhard Rosenberger (http://www.redbb.org)*//*** Include needed Date classes*/ require_once( 'Zend/Date.php' ); class Application_myScripts_Date extends Zend_Date{ private $useminuteago = true; private $usehour = true; private $usetoday = true; private $useyesterday = true; public function getRelativeDate( $timestamp=null, $format=null, $config=null, $part=null, $locale=null ) { if ( $timestamp === null ) { $timestamp = time();} self::set( $timestamp );// To we have own settings? if ( is_array( $config ) ) { foreach( $config AS $key => $value ) { $this->$key = $value;}}// Try to get a Translator// It must be set as Zend_translate in the registry,// else it will not work and standard english will be returned $translate = null; if ( Zend_Registry::isRegistered( 'Zend_Translate' ) ) { $translate = Zend_Registry::get( 'Zend_Translate' );}// Check if the timestamp is smaller than 1 minute ago if ( time() - $timestamp < 61 && $this->useminuteago ) {// its smaller than one minute if ( $translate === null ) { return "A minute ago"; } else { return $translate->translate( "A minute ago" );}}// Check if the timestamp is smaller than 60 minutes if ( time() - $timestamp < 3600 && $this->usehour ) {// its a in hour date, return it $minute = round( ( time() - $timestamp ) / 60 ); if ( $translate === null ) { return $minute . " minutes ago"; } else { return $minute . $translate->translate( " minutes ago" );}}// Check if the timestamp is today if ( self::isToday() && $this->usetoday ) {// Its today if ( $translate === null ) { return "Today, " . self::get( Zend_Date::TIME_SHORT ); } else { return $translate->translate( "Today" ) . ", " . self::get( Zend_Date::TIME_SHORT );}}// Check if the timestamp is yesterday if ( self::isYesterday() && $this->useyesterday ) {// Its yesterday if ( $translate === null ) { return "Yesterday, " . self::get( Zend_Date::TIME_SHORT ); } else { return $translate->translate( "Yesterday" ) . ", " . self::get( Zend_Date::TIME_SHORT );}}// Nothing until now, return as normal if ( $format === null ) {// No special format, return default return self::get(); } else { return self::get( $format );}}}
Here a short Tutorial on how to use this class.
In your bootstrap or index file first load the neccesary files.
Zend_Loader::loadClass( 'Zend_Date' ); require_once( 'Application/myScripts/Date.php');
Now you can call the script in any part of your application
// Use all in default mode $date = new Application_myScripts_Date(); $formateddate = $date->getRelativeDate( $timestamp );// Use a special Date format if no relative date is found// The Constants available for the format argument are the same// as in the normal Zend_Date Class (see Zend_Date documentation) $date = new Application_myScripts_Date(); $formateddate = $date->getRelativeDate( $timestamp, Zend_Date::DATES );// Disable today and yesterday displaymodes// Further posibilities are:// - useminuteago// - usehour $date = new Application_myScripts_Date(); $config = array( 'usetoday' => false, 'useyesterday' => false ); $formateddate = $date->getRelativeDate( $timestamp, Zend_Date::DATES, $config );
Translating relative Dates:
If you use Zend_Translate in your project, make sure it is set in the Zend_Registry with the key Zend_Translate. It will be called automaticly when the key is set.
MessageIDs you need in your Translations:
One Response for "Zend_Date – get Relative"
Thanks for that!
Leave a reply