繁体   English   中英

日期时间转换的 SQLSRV PHP 插入问题

[英]SQLSRV PHP Insert Problem with datetime conversion

当我尝试输入日期时,在 sql server 数据库中执行插入查询时遇到问题。 我遇到的问题是:

插入日期时间时从字符串转换日期和/或时间时转换失败

我正在使用的查询是:

$sqlpedido = "
    INSERT INTO dbo.I_Pedido (id_cliente, Fecha, total, Comentario, portes, estado, FPago, id_destino, FormaEnvio) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
"; 
$params = array($ClienteID, $newformat, $gtotal, $cusnote, $shiptotal, '0', $paymethod,$id_destino, $shipmethod);
$stmt26 = $conn5->prepare($sqlpedido);  
$stmt26->execute($params);

问题出现在变量$newformat ,我尝试了不同的形式将 Woocommerce 提供的日期字符串转换为日期时间:

选项1:

$dcreated = $order->get_date_created();
$d = new DateTime($dcreated);
$timestamp = $d->getTimestamp();
$formatted_date = $d->format('c');

选项 2:

$dcreated = $order->get_date_created();
$time = strtotime($dcreated);
$newformat = date('c',$time);

但是这些选项都没有解决问题......你能帮我吗?

谢谢!!

您需要使用一个明确的日期\\时间格式( yyyy-mm-ddThh:mm:ss ),当您传递日期\\时间值作为文本datetime在SQL Server列。

函数get_date_created()返回WooCommerce DateTime 对象,因此使用 PHP DateTime::format方法格式化值是一个选项:

<?php

...
$newformat = $order->get_date_created()->format('Y-m-d\TH:i:s');

$sqlpedido = "
   INSERT INTO dbo.I_Pedido (id_cliente, Fecha, total, Comentario, portes, estado, FPago, id_destino, FormaEnvio) 
   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
"; 
$params = array($ClienteID, $newformat, $gtotal, $cusnote, $shiptotal, '0', $paymethod, $id_destino, $shipmethod);
$stmt26 = $conn5->prepare($sqlpedido);  
$stmt26->execute($params);
...

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM