从typecho中剥了一段词义化时间代码并按需做了修改,在测试中发现有个问题很纳闷:(假如Unix时间戳$form与$now相差300,而它们处于不同的小时段idate(H),这样5分钟前的操作却显示为1小时前,然而在TE中却没有发现这现象^-!,现在的问题是$between<3600而idate(H)相差1,请问如何修正?本人PHP门外汉,特不耻上问!if($between<3600&&idate('H',$from)==idate('H',$now)){functiondateWord($from){$now=time();$between=$now-$from;$s=date('Y年m月d日H:i',$from);if($between>0&&$between<86400&&idate('d',$from)==idate('d',$now)){if($between<3600&&idate('H',$from)==idate('H',$now)){if($between<60&&idate('i',$from)==idate('i',$now)){$second=idate('s',$now)-idate('s',$from);if(0==$second){return'刚刚'; }else{return''.$second.'秒前'; }}$min=idate('i',$now)-idate('i',$from);return''.$min.'分钟前'; }$hour=idate('H',$now)-idate('H',$from);return''.$hour.'小时前'; }}源码如下/***词义化时间**@accesspublic*@paramstring$from起始时间*@paramstring$now终止时间*@returnstring*/publicstaticfunctiondateWord($from,$now){$between=$now-$from;/**如果是一天*/if($between>0&&$between<86400&&idate('d',$from)==idate('d',$now)){/**如果是一小时*/if($between<3600&&idate('H',$from)==idate('H',$now)){/**如果是一分钟*/if($between<60&&idate('i',$from)==idate('i',$now)){$second=idate('s',$now)-idate('s',$from);if(0==$second){return_t('刚刚');}else{returnsprintf(_n('%d秒前','%d秒前',$second),$second);}}$min=idate('i',$now)-idate('i',$from);returnsprintf(_n('%d分钟前','%d分钟前',$min),$min);}$hour=idate('H',$now)-idate('H',$from);returnsprintf(_n('%d小时前','%d小时前',$hour),$hour);}/**如果是昨天*/if($between>0&&$between<172800&&(idate('z',$from)+1==idate('z',$now)||idate('z',$from)>2+idate('z',$now))){return_t('昨天%s',date('H:i',$from));}/**如果是一个星期*/if($between>0&&$between<604800&&idate('W',$from)==idate('W',$now)){$day=intval($between/(3600*24));returnsprintf(_n('%d天前','%d天前',$day),$day);}/**如果是*/if($between>0&&$between<31622400&&idate('Y',$from)==idate('Y',$now)){returndate(_t('n月j日'),$from);}returndate(_t('Y年m月d日'),$from);}
慕森卡
回首忆惘然
相关分类