将带有日期和时间的PHP字符串转换为SQL时间戳


converting php string with date and time to sql timestamp

我有一个php字符串,其中包含如下所示的日期和时间

2015-05-27 18:45:31

我怎么把它转换成一个类型,我可以插入到postgresql表与时间戳数据类型?

使用strtotime()函数进行转换:

$unix_timestamp=strtotime('2015-05-27 18:45:31');

在你的SQL查询中使用$unix_timestamp

也可以在SQL语句中使用TIMESTAMP '2015-05-27 18:45:31'表示法

如果您有其他日期格式,您可以使用DateTime::createFromFormat

在你的例子中:

$dateString = '2015-05-27 18:45:31';
$format = 'Y-m-d H:i:s';
$dateConvert = DateTime::createFromFormat($format, $dateString);
echo "Date: $format; " . $dateConvert->format('Y-m-d H:i:s') . "'n";

输出类似于:

Date: Y-m-d H:i:s; 2015-05-27 18:45:31