>我有一个Postgres表,如下所示:
create table test
(
time_stamp timestamp
);
我想用 PHP 生成一个字符串,它将当前时间、毫秒(或微秒)插入表中。
根据其他一些研究,我尝试插入以下结果:
date('Y-m-d H:i:s.u', time())
但我只有几秒钟。有什么想法吗?
或者
你可以使用日期:
list($usec, $sec) = explode(' ', microtime());
$usec = str_replace("0.", ".", $usec);
print date('H:i:s', $sec) . $usec;
我以为在 PHP 中有一种简单的方法可以做到这一点,但显然没有。这是解决方案:
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;
}
我正在使用类日期时间 [1] 中的date_create [2]
$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
[2]https://www.php.net/manual/en/class.datetime.php