我要围着这一个转圈!我正在做以下事情:
我在这里遇到问题。
如果我使用echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
哪个好,但是当我在SQL查询中使用它时,我没有得到我期望的结果,所以我认为最好的办法是确保它首先被识别为日期。
如果我使用date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
如果我使用strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
如果我使用date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
如果我将date()与strtotime()一起使用:
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
我确信我完全错过了一些简单的事情。
编辑:我尝试了一些我发现的新功能:$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'
和
$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
编辑2:
我尝试过使用CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
但是失败并出现错误“将varchar数据类型转换为日期时间数据类型导致超出范围的值”
答案 0 :(得分:1)
这些可能有助于揭示你正在寻找的东西。
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.aspx
答案 1 :(得分:0)
如果从MSSQL表中检索日期并且您想在PHP中使用strtotime()
并且也不想将日期格式更改为yyyy-mm-dd那么您可以使用
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL