登录  /  注册

datetime - PHP词义化时间 idate

php中文网
发布: 2016-06-06 20:46:20
原创
1029人浏览过

从typecho中剥了一段词义化时间代码并按需做了修改,在测试中发现有个问题很纳闷:(
假如Unix时间戳$form与$now相差300, 而它们处于不同的小时段idate(H), 这样5分钟前的操作却显示为1小时前, 然而在TE中却没有发现这现象^-!,
现在的问题是$between

if ($between

<code>function dateWord($from) {
$now = time();
$between = $now - $from;
$s = date('Y年m月d日 H:i', $from);
if ($between &gt; 0 &amp;&amp; $between 刚刚';
            } else {
                return '<span title="'.$s.'">'.$second.' 秒前</span>';
            }
        }
        $min = idate('i', $now) - idate('i', $from);
        return '<span title="'.$s.'">'.$min.' 分钟前</span>';
    }
    $hour = idate('H', $now) - idate('H', $from);
    return '<span title="'.$s.'">'.$hour.' 小时前</span>';
}
</code>
登录后复制
登录后复制

}

源码如下

<code>    /**
 * 词义化时间
 *
 * @access public
 * @param string $from 起始时间
 * @param string $now 终止时间
 * @return string
 */
public static function dateWord($from, $now)
{
    $between = $now - $from;

    /** 如果是一天 */
    if ($between &gt; 0 &amp;&amp; $between  0 &amp;&amp; $between  2 + idate('z', $now))) {
        return _t('昨天 %s', date('H:i', $from));
    }

    /** 如果是一个星期 */
    if ($between &gt; 0 &amp;&amp; $between  0 &amp;&amp; $between </code>
登录后复制
登录后复制

回复内容:

从typecho中剥了一段词义化时间代码并按需做了修改,在测试中发现有个问题很纳闷:(
假如Unix时间戳$form与$now相差300, 而它们处于不同的小时段idate(H), 这样5分钟前的操作却显示为1小时前, 然而在TE中却没有发现这现象^-!,
现在的问题是$between

if ($between

<code>function dateWord($from) {
$now = time();
$between = $now - $from;
$s = date('Y年m月d日 H:i', $from);
if ($between &gt; 0 &amp;&amp; $between 刚刚';
            } else {
                return '<span title="'.$s.'">'.$second.' 秒前</span>';
            }
        }
        $min = idate('i', $now) - idate('i', $from);
        return '<span title="'.$s.'">'.$min.' 分钟前</span>';
    }
    $hour = idate('H', $now) - idate('H', $from);
    return '<span title="'.$s.'">'.$hour.' 小时前</span>';
}
</code>
登录后复制
登录后复制

}

源码如下

<code>    /**
 * 词义化时间
 *
 * @access public
 * @param string $from 起始时间
 * @param string $now 终止时间
 * @return string
 */
public static function dateWord($from, $now)
{
    $between = $now - $from;

    /** 如果是一天 */
    if ($between &gt; 0 &amp;&amp; $between  0 &amp;&amp; $between  2 + idate('z', $now))) {
        return _t('昨天 %s', date('H:i', $from));
    }

    /** 如果是一个星期 */
    if ($between &gt; 0 &amp;&amp; $between  0 &amp;&amp; $between </code>
登录后复制
登录后复制

因为你剥离出来的代码对小时只对同一小时的情况进行了判断,并未判断相邻小时但相差60分钟内的情况,因此,我加了一个 gapHour 的变量用来代表小时差,当时间相差60分钟内,做一个 if 判断,如果同一小时默认处理($gapHour == 0),相差一个小时时($gapHour == 1),再做相应的处理。

<code>function dateWord($from) {
  $now = time();
  $between = $now - $from;
  $s = date('Y年m月d日 H:i', $from);
  $fromHour = idate('H', $from);
  $nowHour = idate('H', $now);
  $gapHour = $nowHour - $fromHour;
  if ($between &gt; 0 &amp;&amp; $between 刚刚';
                } else {
                    return '<span title="'.$s.'">'.$second.' 秒前</span>';
                }
            }
            $min = idate('i', $now) - idate('i', $from);
            return '<span title="'.$s.'">'.$min.' 分钟前</span>';
          }
          else if ($gapHour == 1) {
            if ($between 刚刚';
                } else {
                    return '<span title="'.$s.'">'.$second.' 秒前</span>';
                }
            }
            $min = idate('i', $now) + (60 - idate('i', $from));
            return '<span title="'.$s.'">'.$min.' 分钟前</span>';          
          }
      }
      $hour = idate('H', $now) - idate('H', $from);
      return '<span title="'.$s.'">'.$hour.' 小时前</span>';
  }
}
</code>
登录后复制

额一来大段代码看着头晕,二来官方现在也不是很推荐用date()函数来操作时间对象了,所以我把整个的代码都改写了一下:

<code>function dateWord($from, $now) {
    $timezone = new DateTimeZone('Asia/Shanghai');
    $now = new DateTime($now, $timezone);
    $from = new DateTime($from, $timezone);
    $between = $now-&gt;diff($from);

    if(!$between-&gt;invert) return false;

    /** 如果超过了一年 **/
    if($between-&gt;y) 
        return $from-&gt;format('Y年m月d日');

    /** 一年内大于七天 **/
    if($between-&gt;days &gt; 6) 
        return $from-&gt;format('n月j日');

    /** 一个礼拜内但是大于两天**/
    if($between-&gt;days &gt; 1)
        return $between-&gt;format('%d天前');

    /** 如果是昨天 **/
    if($between-&gt;days)
        return $from-&gt;format('昨天 H:i');

    /** 如果一天之内超过一个小时 **/
    if($between-&gt;h &gt; 1)
        return $between-&gt;format('%h小时前');

    if($between-&gt;i &gt; 1)
        return $between-&gt;format('%i分钟前');

    return $between-&gt;s ? $between-&gt;format('%s秒前') : '刚刚';
}

function dateWordToHtml($from, $now = 'now') {
    $dateWord = dateWord($from, $now);
    $from = new DateTime($from, new DateTimeZone('Asia/Shanghai'));
    $fromWord = $from-&gt;format('Y年m月d日 H:i');
    return "<span title='\"$fromWord\"'>$dateWord</span>";
}


echo dateWordToHtml("2014/4/14 9:32");
</code>
登录后复制

dateWord()函数对应的是Typecho原版的返回,dateWordToHtml()函数则是对应你修改的那个函数(另外新建一个函数主要是方便其他人参考调用)。

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号