datetime - PHP词义化时间 idate

WBOY
Release: 2016-06-06 20:46:20
Original
1159 people have browsed it

从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 > 0 && $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>
Copy after login
Copy after login

}

源码如下

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

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

    /** 如果是一个星期 */
    if ($between > 0 && $between  0 && $between </code>
Copy after login
Copy after login

回复内容:

从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 > 0 && $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>
Copy after login
Copy after login

}

源码如下

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

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

    /** 如果是一个星期 */
    if ($between > 0 && $between  0 && $between </code>
Copy after login
Copy after login

因为你剥离出来的代码对小时只对同一小时的情况进行了判断,并未判断相邻小时但相差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 > 0 && $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>
Copy after login

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

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

    if(!$between->invert) return false;

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

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

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

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

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

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

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

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


echo dateWordToHtml("2014/4/14 9:32");
</code>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template