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

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

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

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

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

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

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

同时给了一个视图助手类,名字为: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. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值