【问题标题】:How to calulate the difference between a MySQL timestamp and the current time in PHP如何在 PHP 中计算 MySQL 时间戳和当前时间之间的差异
【发布时间】:2011-04-15 11:53:15
【问题描述】:

我正在尝试计算从 MySQL 数据库中检索到的时间戳与当前时间之间的差异。

感谢您的帮助。

【问题讨论】:

标签: php mysql zend-framework datetime timestamp


【解决方案1】:

正如@RemoteSojourner 所提到的,我以 UNIX 时间戳格式(以秒为单位返回时间)获得了当前时间,我从数据库(使用 ORM)获得了时间戳并将其转换为 UNIX 时间戳,然后减去两个时间戳。

            $current_time = strtotime("now");
            $last_access_time = strtotime($this->last_access);
            $inactivity_duration = $current_time - $last_access_time;

【讨论】:

    【解决方案2】:

    这个例子使现在和一小时前有所不同。

    选择 timediff(now(), now() - 间隔 1 小时)

    【讨论】:

      【解决方案3】:

      可以使用strtotime函数将MySQL时间戳解析成Unix时间戳,可以在PHP日期函数中进一步解析或格式化。

      【讨论】:

        【解决方案4】:

        像这样从 mysql 中检索日期时间

        SELECT UNIX_TIMESTAMP(`time_col`) FROM `tablename`...
        

        并将其与 time() 进行比较

        【讨论】:

        • 啊,我假设(从 php + zend 标签)你想在 php 中进行比较
        猜你喜欢
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 2020-05-13
        相关资源
        最近更新 更多