在PHP中使用MSSQL datetime麻烦(通过SQLSRV) [英] Trouble using MSSQL datetime in PHP (via SQLSRV)
问题描述
- 通过SQL / PHP从MSSQL datetime字段获取日期
- 通过querystring将日期发送到新的PHP页面
- 尝试在新的SQL查询中使用该日期
我在这里遇到问题。
如果我使用echo:
echo $ _REQUEST ['rSessionDate'];
输出:15/10/2012
哪个是好的,但是当我使用它一个SQL查询我没有得到我期望的结果,所以我认为最好的做法是确保它被确认为一个日期。
如果我使用date_format():
echo date_format($ _ REQUEST ['rSessionDate'],'Ym-d');
输出:警告:date_format()期望参数1为DateTime,字符串为...
如果我使用strtotime():
echo strtotime($ _ REQUEST ['rSessionDate']);
输出:(没有)
如果我使用date():
echo date('Ymd H:i',$ _ REQUEST ['rSessionDate']);
输出:注意:在...中遇到的非正式形式的数值
如果我使用date()与strtotime():
echo date('Ymd H:i',strtotime($ _ REQUEST ['rSessionDate ']));
输出:1970-01-01 01:00
我确定我是完全缺少一些简单的东西。
编辑:我尝试了一些我发现的新功能:
$ rSessionDate = new DateTime($ _ REQUEST ['rSessionDate']);
echo $ rSessionDate-> format('Y-m-d H:i:s');
输出:致命错误:未捕获的异常异常与消息'DateTime :: __ construct():无法解析时间字符串(15/10/2012)在位置0(1):意外的字符'
和:
$ rSessionDate = date_create($ _ REQUEST ['rSessionDate']);
echo date_format($ rSessionDate,'Y-m-d H:i:s');
输出:警告:date_format()期望参数1为DateTime,布尔给定i
编辑2:
我试过使用CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode ='4B'AND(ISNULL(fValidStart,0)< = CAST('2012-10-15 00:00:00'as DATETIME))
但是这样就会出现错误将varchar数据类型转换为datetime数据类型导致超出范围值
解决方案这些可能有助于揭示您正在寻找的内容。
http:// www。 ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime()在上面的例子中返回一个历元时间戳。
或检查CAST和CONVERT(指的是MSSQL2000,但可能仍然帮助你)
http: //msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspxI am going round in circles with this one! I'm doing the following:
- Retrieving a date from an MSSQL datetime field via SQL/PHP
- Sending the date to a new PHP page via the querystring
- Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate']; output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d'); output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']); output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']); output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate'])); output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']); echo $rSessionDate->format('Y-m-d H:i:s'); output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']); echo date_format($rSessionDate, 'Y-m-d H:i:s'); output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
解决方案These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx这篇关于在PHP中使用MSSQL datetime麻烦(通过SQLSRV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!