PDO从Postgres获得分数秒 [英] PDO get fractional seconds from Postgres

查看:97
本文介绍了PDO从Postgres获得分数秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用简单的pdo语句(PHP7)

postgresql (9.6)中查询日期时间字段时

$sql = "SELECT date FROM table"
$stmt = $adapter->createStatement($sql);
$stmt->prepare($sql);
$stmt->execute();

我收到的字符串是2017-02-03 15:51:44而不是2017-02-03 15:51:44.240000(最后一个输出是来自psql控制台的结果)

我看到一则帖子在谈论 解决方案

任何人都找到了一种解决方案来检索php中的小数秒吗?

它们通常是结果的一部分. 这是使用默认的php-7.1.2安装来检索时间戳的实际代码:

<?php
$link = new PDO("pgsql:dbname=test;host=localhost", $user, $password);

$stmt = $link->prepare("SELECT '2016-01-02 22:44:31.625129'::timestamp AS t");
$stmt->execute();
$result = $stmt->fetch();
print_r($result);
?>

结果:

Array
(
    [t] => 2016-01-02 22:44:31.625129
    [0] => 2016-01-02 22:44:31.625129
)

因此,保留这些微秒没有什么特别的事情,可以将它们丢弃,而需要一些额外的代码,或者在SQL中进行截断或强制转换为timestamp(0).

When I query datetime fields from postgresql (9.6) with a simple pdo statement (PHP7)

$sql = "SELECT date FROM table"
$stmt = $adapter->createStatement($sql);
$stmt->prepare($sql);
$stmt->execute();

I receive the string 2017-02-03 15:51:44 instead of 2017-02-03 15:51:44.240000 (the last outpout is result from psql console)

I saw a post talking about this bug report FROM 2011 about mysql and php 5, I guess since 2011 the problem have been fixed or people have find a way to fix this.

Anyone found a solution to retrieve the fractional seconds in php ?

解决方案

Anyone found a solution to retrieve the fractional seconds in php ?

They're normally part of the result. Here's actual code that retrieves a timestamp with a default php-7.1.2 install:

<?php
$link = new PDO("pgsql:dbname=test;host=localhost", $user, $password);

$stmt = $link->prepare("SELECT '2016-01-02 22:44:31.625129'::timestamp AS t");
$stmt->execute();
$result = $stmt->fetch();
print_r($result);
?>

Result:

Array
(
    [t] => 2016-01-02 22:44:31.625129
    [0] => 2016-01-02 22:44:31.625129
)

So there's nothing special to do to keep these microseconds, it would be to discard them that some additional code would be needed, or a truncate in SQL or a cast to timestamp(0).

这篇关于PDO从Postgres获得分数秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆