[英]Insert timestamp with milliseconds to Postgres table with PHP
I have a Postgres table, like so:我有一个 Postgres 表,如下所示:
create table test
(
time_stamp timestamp
);
I want to generate a string with PHP that will insert the current time, with milliseconds (or microseconds) into the table.我想用 PHP 生成一个字符串,该字符串将在表中插入当前时间,以毫秒(或微秒)为单位。
Based on some other research, I've tried inserting the results of:根据其他一些研究,我尝试插入以下结果:
date('Y-m-d H:i:s.u', time())
But I'm only getting seconds.但我只有几秒钟。 Any ideas?
有任何想法吗?
Or u can use date: 或者您可以使用日期:
list($usec, $sec) = explode(' ', microtime());
$usec = str_replace("0.", ".", $usec);
print date('H:i:s', $sec) . $usec;
I had assumed there was an easy way to do this in PHP, but apparently there's not. 我以为有一种简单的方法可以在PHP中执行此操作,但显然没有。 Here's the solution:
解决方法如下:
function get_time() {
$time = microtime(true);
$micro_time = sprintf("%06d", ($time - floor($time)) * 1e6);
return date('Y-m-d H:i:s.', $time).$micro_time;
}
I'm using date_create [1] from class datetime [2]我正在使用 datetime [2] 类中的 date_create [1]
$dt1 = date_create();
echo $dt1->format('Y-m-d H:i:s.u');
2021-09-09 11:12:04.292321
[1] https://www.php.net/manual/en/function.date-create [1] https://www.php.net/manual/en/function.date-create
[2] https://www.php.net/manual/en/class.datetime.php [2] https://www.php.net/manual/en/class.datetime.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.