【问题标题】:A PostgreSQL timestamp field not formated in the same way when updated with PHP使用 PHP 更新时,PostgreSQL 时间戳字段的格式不同
【发布时间】:2009-04-06 20:05:14
【问题描述】:

我在 PostgreSQL 中有一个 timestamp with timezone 字段。 当我更新这个字段时,我会使用这样的东西:

$date = date('Y-m-d H:i:s');

虽然 SQL 运行良好,但保存的日期似乎与经典的 timestamp with timezone 日期有点不同。

Example:

Default value set to "now()":
date 2009-04-06 14:39:53.662522+02

Update with a date set in php:
$date = date('Y-m-d H:i:s');
date 2009-04-06 14:39:53+02

更新时删除的数字可能是毫秒,但我不确定。 我想知道是否有办法用PHP获得相同格式的日期?

【问题讨论】:

    标签: php postgresql date timestamp


    【解决方案1】:

    如果您只需要一秒的时间戳分辨率,则必须相应地设计您的数据库,因为默认分辨率优于一秒。

    例如使用以下列定义:

    last_access_time timestamp with time zone not null
        default date_trunc('second',now())
        constraint last_access_time_full_second check (
            date_trunc('second',last_access_time)=last_access_time
        )
    

    【讨论】:

    • 幸运的是,列定义可以更简单(至少是默认值的一部分):last_access_time timestamp(0) with time zone default CURRENT_TIMESTAMP
    【解决方案2】:

    您可以使用the microtime() PHP function 获取微秒数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2013-09-17
      • 2015-02-20
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多