打印一小时前的时间

发布于 2025-01-07 13:27:21 字数 427 浏览 2 评论 0原文

可能的重复:
给定时间,我怎样才能找到一个月前的时间

如何使用 Date 在 PHP 中打印一小时前的时间?

$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;

所以如果我想说“约翰最后访问过”

会打印

约翰于 2012 年 2 月 20 日 17.26 访问过

Possible Duplicate:
Given a time, how can I find the time one month ago

How can I print an hour ago in PHP using Date?

$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;

So If I wanted to say "John visited last "

Would print

John visited last 20th Feb 2012, 17.26

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

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

发布评论

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

评论(4

聊慰 2025-01-14 13:27:22
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
抱猫软卧 2025-01-14 13:27:22
$date = date("Y-m-d H:i:s", time() - 3600);

时间() ->当前时间戳

时间减去 3600 秒,即 1 小时前的时间。要设置日期格式,您可以在此处查找选项:http://php. net/manual/en/function.date.php

或者,您可以使用以下格式:

$date = date("Y-m-d H:i:s", strtotime('-1 hour'));

尽管如果您想删除更具体的时间单位(例如一天和 3 小时),则使用该方法可能会有点笨拙)。

如果我明白你想要做什么的话那就是这样。

$date = date("Y-m-d H:i:s", time() - 3600);

time() -> Current timestamp

Time minus 3600 seconds, is the time 1 hour ago. To get the date formatted, you can look here for the options: http://php.net/manual/en/function.date.php

Alternatively you could use the following format:

$date = date("Y-m-d H:i:s", strtotime('-1 hour'));

Though using that method can be a little clunky if you want to remove more specific units of time (Such as one day and 3 hours).

Thats if I've understood what you want to do correctly that is.

笔落惊风雨 2025-01-14 13:27:22

我想你会从 mysql 获取日期和时间,最好的办法是使用 mysql 的 DATE_FORMAT 函数并计算出来。

否则,在简单的 php 中你可以这样做
$date=date("Ymd H:i:s", $time -3600);

更好的选择是像这样使用 strtotime
$date=date("Ymd H:i:s", strtotime('-1 小时'));

并完成工作。

I suppose you would be fetching date and time from mysql and the best thing to do is using mysql's DATE_FORMAT function and work out.

Other wise in simple php you could do it like this
$date=date("Y-m-d H:i:s", $time -3600);

Better option is to use strtotime like this one
$date=date("Y-m-d H:i:s", strtotime('-1 hour'));

And get the work done.

拥抱我好吗 2025-01-14 13:27:22

嗯,搜索一下我使用的功能的手册。您缺少一些有关 PHP 日期/时间函数的信息...

// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);

$result = $date;

Mmm, search the manual the function I used. You are missing something about PHP date/time functions...

// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);

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