【问题标题】:Print the time an hour ago [duplicate]打印一小时前的时间[重复]
【发布时间】:2023-04-10 00:54:01
【问题描述】:

可能重复:
Given a time, how can I find the time one month ago

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

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

所以如果我想说“约翰上次访问过”

会打印

John 于 2012 年 2 月 20 日 17:26 访问过

【问题讨论】:

  • 继续在谷歌上快速搜索“php 相对时间函数”

标签: php string date time date-arithmetic


【解决方案1】:
$date = date("Y-m-d H:i:s", time() - 3600);

time() -> 当前时间戳

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

您也可以使用以下格式:

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

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

如果我已经正确理解了你想要做什么的话。

【讨论】:

    【解决方案2】:
    $date = date('Y-m-d H:i:s', strtotime('-1 hour'));
    echo 'John visited last ' . $date;
    

    【讨论】:

      【解决方案3】:

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

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

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

      并完成工作。

      【讨论】:

        【解决方案4】:

        嗯,在手册中搜索我使用的功能。你错过了一些关于 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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-03
          • 2017-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多