简体   繁体   English

PHP输入MS SQL无法从字符串转换日期和/或时间

[英]PHP Input MS SQL Failed to convert date and/or time from character string

I'm running a MSSQL function and returning to PHP and I'm using the function twice in the PHP and writing it away to a MySQL database. 我正在运行MSSQL函数并返回PHP,我在PHP中使用该函数两次并将其写入MySQL数据库。

The first time I run the function I'm using $to and $from (I'm well aware it's not pretty, I'm not a php coder by trade...) 我第一次运行函数我使用$ to和$ from(我很清楚它不漂亮,我不是交易的php编码器......)

$to = date("j M Y", mktime());
$year = date("Y", mktime());
$jan = '1 jan ';
$from = $jan . $year;

I'm then using the following 我正在使用以下内容

select * from MISYearToDate('$from', '$to')

and that works absolutely fine. 这非常好。

When I then do the same thing but putting in this week's dates using 然后当我做同样的事情,但在本周的日期使用

$weekstarting = date('j M Y', strtotime('last sunday'))."<br>";
$weekending = date('j M Y', strtotime('next saturday'))."<br>";

and then run 然后跑

select * from MISYearToDate('$weekstarting', '$weekending')

I'm getting the error 我收到了错误

[Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string. ) 

$From and $to echo $ From和$回声

1 jan 2012
27 Jun 2012

$weekstarting and $weekending echo $ weekstarting和$ weekend echo

24 Jun 2012
30 Jun 2012

So as far as I can tell, there really isn't a difference and I'm pretty damn confused! 所以据我所知,确实没有什么区别,我真的很困惑!

I'd recommend only feeding dates into SQL in the format of yyyy-mm-dd. 我建议只以yyyy-mm-dd格式将日期输入SQL。 This will ensure SQL doesn't get confused with the date format. 这将确保SQL不会与日期格式混淆。

SQL will not accept dates in the format of 1 jan 2012 (as a string). SQL不接受格式为2012年1月1日的日期(作为字符串)。

$weekending = date('j M Y', strtotime('next saturday'))."<br>";
                                                       ^^^^^^^

<br> is not a valid component of an SQL date string. <br>不是SQL日期字符串的有效组件。 You're probably viewing the output in a browser, so the <br> is effectively hidden, but that's where your problem is. 您可能正在浏览器中查看输出,因此有效隐藏了<br> ,但这就是您的问题所在。

That's turning your query into: 这会将您的查询转变为:

select * from MISYearToDate('24 Jun 2012<br>', '30 Jun 2012<br>')

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

相关问题 使用 PHP 和 PDO 与 MS SQL 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string using PHP and PDO with MS SQL 转换输入的时间时从字符串转换日期和/或时间时转换失败| SQL,SQLSRV - Conversion failed when converting date and/or time from character string when converting inputted time | SQL, SQLSRV 转换日期并从字符串日期php中删除时间 - Convert date and remove time from a string date php PHP 将字符串转换为日期时间 - PHP convert string into date time 在PHP中转换日期时间字符串 - Convert date time string in PHP 无法更正我的代码:从字符串转换日期和/或时间时转换失败 - Failed to correct my codes: Conversion failed when converting date and/or time from character string 将输入的日期和时间转换为sql datetime - Convert date and time input to sql datetime 从字符串错误转换日期和/或时间时,MSSQL Server 2008转换失败 - MSSQL Server 2008 Conversion failed when converting date and/or time from character string Error 如何修复“未捕获的 PDOException:...从字符串转换日期和/或时间时转换失败”? - How to fix "Uncaught PDOException: ...Conversion failed when converting date and/or time from character string"? 从字符串转换日期和/或时间时转换失败 - 但不明白为什么 - Conversion failed when converting date and/or time from character string - but no idea why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM