日期和当前时间之间的时差?

发布于 2024-12-10 16:23:56 字数 607 浏览 0 评论 0原文

假设我有一个有趣的视频网站,向用户展示有趣的视频。在每个视频下方,我希望有一个声明,根据视频的时间戳和当前时间显示“5 秒前”、“31 分钟前”、“昨天”、“2 天前”。

我的时间戳格式为:2011-10-17 07:08:00

我不太擅长日期,但我猜我需要找到两个日期/时间之间的差异(以秒为单位),然后确定它是否在 0 秒和 0 秒之间。 60sec 以秒为单位显示,或在 60sec 和 60sec 之间显示3600秒以分钟为单位显示,或在3600秒与3600秒之间显示3600x24秒以小时为单位显示,介于3600x24秒和3600x24秒之间3600x24x2sec 昨天显示,依此类推...我相信我应该使用 strtotime() 但我似乎无法找到当前时间,因为我发现的那些解决方案使用 new date()< /code> 这似乎不起作用!

我该怎么做?

顺便说一句,附带问题,当我将 2011-10-17, 7:08PM EDT 插入 MySQL 时间戳列时,它会转换为 2011-10-17 07:08:00代码> 这是AM。如何才能将其存储在正确的 AM/PM 中?

Assume I have a funny video site that shows funny videos to users. Below every video, I want to have a statement that says "5 seconds ago", "31 minutes ago", "Yesterday", "2 days ago" based on a timestamp of the video and the current time.

The timestamp that I have is in the format: 2011-10-17 07:08:00.

I'm not too good with dates, but I'm guessing that I need to find the difference between the 2 date/time in seconds, then decide if its between 0sec & 60sec to display in seconds, or between 60sec & 3600sec to display in minutes, or between 3600sec & 3600x24sec to display in hours, between 3600x24sec & 3600x24x2sec to display yesterday, and so on... I believe I should be using strtotime() but I cant seem to find the current time as those solutions I found used new date() which does not seem to work!

How can I do this?

Btw, side question, when I insert 2011-10-17, 7:08PM EDT into a MySQL timestamp column, it gets converted to 2011-10-17 07:08:00 which is AM. How can I get it to be stored in the correct AM/PM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十二 2024-12-17 16:23:56

您可以使用php的DateTime函数。

$datetime1 = new DateTime('2011-10-17 07:08:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

从这里开始,您可以使用一些 if 语句根据实际时差以另一种格式(秒、分钟、小时、月等)输出时差!输出的格式可以在此处找到

You can use the DateTime functions of php.

$datetime1 = new DateTime('2011-10-17 07:08:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

From here on you can use some if-statements to output the time difference in another format (seconds, minutes, hours, month, etc.) depending on the actual time difference! The formats for the output are to find here

笔落惊风雨 2024-12-17 16:23:56

您可以非常轻松地使用 DATEDIFF 和 TIMEDIFF 函数MySQL 的。两者一起告诉您确切已经过去了多少时间。

You can very easily use the DATEDIFF and TIMEDIFF functions of MySQL. Both together tell you exactly how much time has passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文