我正在尝试构建一个带有时间戳并生成基于“相对”字符串的日期(可能是反向strtotime)的函数!
Yesterday at 2pm
Yesterday at 3:15pm
Earlier Today at 9am
Today at 12pm
Today at 12:45pm
Tomorrow at 5pm
Monday at 9pm
Sunday at 12:55am
All dates older than a week:
Thu 7th Jul
我目前有以下代码,并且想知道如何改进它?它似乎效率不高,缺少一些功能.您会添加什么?
static public function timestampRelativeString($timestamp, $rounding=900) {
if(!self::isValidTimeStamp($timestamp))
return "Invalid Timestamp";
//Round to (default) 15 mins
$timestamp = floor($timestamp/$rounding)*$rounding;
//Today
if($timestamp<mktime(0, 0, 0, date("m"), date("d")-1, date("y")))
return date("D jS M", $timestamp);
elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")-1, date("y")))
return "Today at ".date("G:i", $timestamp);
elseif($timestamp<time()))
return "Earlier today at ".date("G:i", $timestamp);
elseif($timestamp<mktime(23, 59, 59, date("m"), date("d"), date("y")))
return "Today at ".date("G:i", $timestamp);
elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")+1, date("y"))) //Tomorrow
return "Tomorrow at ".date("G:i", $timestamp);
elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")+7, date("y"))) //Next Week
return "On ".date("l", $timestamp)." at ".date("G:i", $timestamp);
else
return date("D jS M", $timestamp);
}
有谁知道已经执行此操作的脚本?还是很相似?
非常感谢您的宝贵时间,
解决方法:
它有很多名称,包括nicetime和PrettyDate.最好的方法是从PHP中以ID /标识类在div / span中输出实际时间,然后用javascript(可随后更新用户坐在页面上的时间).
无论如何,WRT到您的问题,我将看看其中的prettydate处理类,它们类似于(或完全一样)您在Facebook,Twitter等上看到的类.即使您使用PHP进行操作,您也可以匹配该功能.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。