gpt4 book ai didi

PHP 将日期转换为消息,例如 Yesterday at 2 :00pm - Reverse strtotime

转载 作者:搜寻专家 更新时间:2023-10-31 20:51:52 25 4
gpt4 key购买 nike

我正在尝试构建一个函数,该函数采用时间戳并生成基于“相对”字符串的日期,可能是反向 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);
}

有人知道已经执行此操作的脚本吗?还是非常相似?

非常感谢您的宝贵时间,

最佳答案

它有很多名字,包括 nicetimePrettyDate .最好的方法是在带有 ID 或标识类的 div/span 中从 PHP 输出实际时间,然后使用 javascript 对客户端进行后处理(然后可以在用户坐在页面上时更新时间) .

无论如何,关于你的问题,我会看看那里的 prettydate 处理类,它们就像(或完全)你在 Facebook、Twitter 等上看到的那些。即使你用 PHP 做,你也可以在匹配功能。

关于PHP 将日期转换为消息,例如 Yesterday at 2 :00pm - Reverse strtotime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6653742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com