[英]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.