呵,首先说一下,为什么写了三天了,序号还是“一”呢?
因为我感觉其实这些只能算是一节里的内容。看别人的博客也是写在一篇里的。呵
今天说的是视图助手,建议大家先看一下这篇日志.
那里说的已经很清楚了,至少我是学到了不少东西。
“视图助手”可以在页面上直接调用,很是方便,
在这里给大家一个日期处理类吧,计算给定日期与现在时间的差,
同时给了一个视图助手类,名字为:DateFormat.php
这时在视图里面就可以直接调用$this->dateFormat('2008-10-15 12:35:27'),来输出结果了。
结果为:
string '10小时以前' (length=10)
- <?php
- /*
- * input params: unix timestamp, use strtotime
- * */
- class QDateTime
- {
- private function QDateTime(){}
- public static function getInstance()
- {
- static $ins ;
- if (false == $ins instanceof QDateTime)
- {
- $ins = new QDateTime();
- }
- return $ins ;
- }
- public static function dayDiff( $fromTime , $toTime )
- {
- return self::convertResult(( $fromTime - $toTime ) / 86400 );
- }
- public static function hourDiff( $fromTime , $toTime )
- {
- return self::convertResult(( $fromTime - $toTime ) / 3600 );
- }
- public static function minuteDiff( $fromTime , $toTime )
- {
- return self::convertResult(( $fromTime - $toTime ) / 60 );
- }
- public static function secondDiff( $fromTime , $toTime )
- {
- return self::convertResult( $fromTime - $toTime );
- }
- public static function dayDiffFromNow( $time )
- {
- return self::convertResult((time() - $time ) / 86400);
- }
- public static function hourDiffFromNow( $time )
- {
- return self::convertResult((time() - $time ) / 3600);
- }
- public static function minuteDiffFromNow( $time )
- {
- return self::convertResult((time() - $time ) / 60);
- }
- public static function secondDiffFromNow( $time )
- {
- return self::convertResult(time() - $time );
- }
- private static function convertResult( $result )
- {
- if ( $result < 0)
- {
- $result = (-1) * $result ;
- }
- return $result ;
- }
- public static function getDateNow( $format = '%Y%m%d' )
- {
- return strftime ( $format , time());
- }
- public static function getYearNow()
- {
- return self::getDateNow( "%Y" );
- }
- public static function getMonthNow()
- {
- return self::getDateNow( "%m" );
- }
- public static function getDayNow()
- {
- return self::getDateNow( "%d" );
- }
- }
- <?php
- require_once ( "qdatetime.php" );
- class Zend_View_Helper_DateFormat
- {
- public function DateFormat( $date )
- {
- $date = strtotime ( $date );
- $q = QDateTime::getInstance();
- $min = $q ->minuteDiffFromNow( $date );
- if (1 > $min )
- {
- return $q ->secondDiffFromNow( $date ). '秒以前' ;
- }
- else if (60 > $min )
- {
- return floor ( $min ). '分钟以前' ;
- }
- else if (24 >= $hour = $q ->hourDiffFromNow( $date ))
- {
- return floor ( $hour ). '小时以前' ;
- }
- else if (24 < $hour = $q ->hourDiffFromNow( $date ))
- {
- return date ( 'm月d日' , $date );
- }
- else
- {
- return date ( 'Y年m月d日' , $date );
- }
- }
- }