`
opensuse
  • 浏览: 183223 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ZEND FRAMEWORK学习笔记:一.视图助手

    博客分类:
  • php
阅读更多

呵,首先说一下,为什么写了三天了,序号还是“一”呢?

 

因为我感觉其实这些只能算是一节里的内容。看别人的博客也是写在一篇里的。呵

 

今天说的是视图助手,建议大家先看一下这篇日志.

 

那里说的已经很清楚了,至少我是学到了不少东西。

 

“视图助手”可以在页面上直接调用,很是方便,

 

在这里给大家一个日期处理类吧,计算给定日期与现在时间的差,

 

同时给了一个视图助手类,名字为:DateFormat.php

 

这时在视图里面就可以直接调用$this->dateFormat('2008-10-15 12:35:27'),来输出结果了。

 

结果为:

 


string '10小时以前' (length=10)

 

  1. <?php
  2. /*
  3.  * input params: unix timestamp, use strtotime 
  4.  * */
  5. class  QDateTime
  6. {
  7.      private   function  QDateTime(){}
  8.      public   static   function  getInstance()
  9.     {
  10.          static   $ins ;
  11.          if  (false ==  $ins  instanceof QDateTime)
  12.         {
  13.              $ins  =  new  QDateTime();
  14.         }
  15.          return   $ins ;
  16.     }
  17.         
  18.      public   static   function  dayDiff( $fromTime $toTime )
  19.     {
  20.          return  self::convertResult(( $fromTime  -  $toTime ) / 86400 );
  21.     }
  22.     
  23.      public   static   function  hourDiff( $fromTime $toTime )
  24.     {
  25.          return  self::convertResult(( $fromTime  -  $toTime ) / 3600 );
  26.     }
  27.     
  28.      public   static   function  minuteDiff( $fromTime $toTime )
  29.     {
  30.          return  self::convertResult(( $fromTime  -  $toTime ) / 60 );
  31.     }
  32.     
  33.      public   static   function  secondDiff( $fromTime $toTime )
  34.     {
  35.          return  self::convertResult( $fromTime  -  $toTime );
  36.     }
  37.     
  38.      public   static   function  dayDiffFromNow( $time )
  39.     {
  40.          return  self::convertResult((time() -  $time ) / 86400);
  41.     }
  42.     
  43.      public   static   function  hourDiffFromNow( $time )
  44.     {
  45.          return  self::convertResult((time() -  $time ) / 3600);
  46.     }
  47.     
  48.      public   static   function  minuteDiffFromNow( $time )
  49.     {
  50.          return  self::convertResult((time() -  $time ) / 60);
  51.     }
  52.     
  53.      public   static   function  secondDiffFromNow( $time )
  54.     {
  55.          return  self::convertResult(time() -  $time );
  56.     }
  57.     
  58.      private   static   function  convertResult( $result )
  59.     {
  60.          if ( $result  < 0)
  61.         {
  62.              $result  = (-1) *  $result ;
  63.         }
  64.          return   $result ;
  65.     }
  66.         
  67.      public   static   function  getDateNow( $format  =  '%Y%m%d' )
  68.     {
  69.          return   strftime ( $format , time());
  70.     }
  71.      public   static   function  getYearNow()
  72.     {
  73.          return  self::getDateNow( "%Y" );
  74.     }
  75.     
  76.      public   static   function  getMonthNow()
  77.     {
  78.          return  self::getDateNow( "%m" );
  79.     }
  80.     
  81.      public   static   function  getDayNow()
  82.     {
  83.          return  self::getDateNow( "%d" );
  84.     }
  85. }
  1. <?php
  2. require_once ( "qdatetime.php" );
  3. class  Zend_View_Helper_DateFormat
  4. {
  5.      public   function  DateFormat( $date )
  6.     {
  7.          $date  =  strtotime ( $date );
  8.          $q  = QDateTime::getInstance();
  9.          $min  =  $q ->minuteDiffFromNow( $date );
  10.         
  11.         
  12.          if  (1 >  $min )
  13.         {
  14.              return   $q ->secondDiffFromNow( $date ). '秒以前' ;
  15.         }
  16.          else   if  (60 >  $min )
  17.         {
  18.              return   floor ( $min ). '分钟以前'
  19.         }
  20.          else   if  (24 >=  $hour = $q ->hourDiffFromNow( $date ))
  21.         {
  22.              return   floor ( $hour ). '小时以前' ;
  23.         }
  24.          else   if  (24 <  $hour = $q ->hourDiffFromNow( $date ))
  25.         {
  26.              return   date ( 'm月d日' $date ); 
  27.         }
  28.          else
  29.         {
  30.              return   date ( 'Y年m月d日' $date );
  31.         }
  32.     }
  33. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics