php 打印一小时前的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9365583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:40:10  来源:igfitidea点击:

Print the time an hour ago

phpstringdatetimedate-arithmetic

提问by TheBlackBenzKid

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?

如何使用日期在 PHP 中打印一小时前?

$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

John 于 2012 年 2 月 20 日,17.26 上次访问

回答by Paul

$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;

回答by BenOfTheNorth

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

time() -> Current timestamp

time() -> 当前时间戳

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

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

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

那就是如果我理解你想要正确做的事情。

回答by abhig10

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.

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

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

其他明智的在简单的 php 中你可以这样做 $date=date("Ymd 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'));

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

And get the work done.

并完成工作。

回答by lorenzo-s

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

嗯,搜索我使用的功能手册。您缺少有关 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;