PHP实现表明它过去有多长时间了。

PHP实现表明它过去有多长时间了。
本文演示了PHP在过去显示时间的方式:

这里有一种更好的可读方法来显示过去的时间,比如从现在开始的10秒,现在的1天,等等。
功能time_is_older_than($,$ check_time){
$ T = strtolower(T);
time_type美元= substr(preg_replace( / { ^ A-Z } / ',' ',为T),0, 1);
val = intval美元(preg_replace( / { ^ 0-9 } / ',' ',$ T));
$ TS=0;
/ /(S)秒,分钟(M),(d)说,(Y)的耳朵
如果($ time_type = =){ $ TS = $ val;}
如果($ time_type = =我){ $ TS = $ val * 60 };
如果($ time_type = = H){ $ TS = $ val * 60 * 60 };
如果($ time_type = =会){ $ TS = $ * 60 * 60 * 24值};
如果($ time_type = = Y){ $ TS = $ val * 60 * 60 * 24 * 365 };
别的{死('unknown时间格式了!);}
如果($ check_time <(时间)美元(TS)){ return true;}
返回false;
}

使用案例:
要测试:时间戳
(可能来自数据库或其他东西)
时间= 1146722922;
如果检查:长
如果(time_is_older_than('30m,$时间)){
print'the给定的时间戳。日期(L DS F Y H:我:是的);
打印-大于30分钟;
}
{其他
print'the给定的时间戳。日期(L DS F Y H:我:是的);
打印-不大于30分钟;
}
短 /检查:
如果(time_is_older_than('10s,$时间)){打印比10秒;}
如果(time_is_older_than('200m,$时间)){打印比200分钟;;}
如果(time_is_older_than('2h,$时间)){打印是大于2小时;}
如果(time_is_older_than('4d,$时间)){打印已经超过4天;;}
如果(time_is_older_than('1y,$时间)){打印已经超过一年;}

希望本文能对大家的PHP程序设计有所帮助。

tag:长时间有多电脑软件php

相关内容