我正在用这个圈子绕圈!我做了以下内容:在PHP中使用MSSQL datetime时遇到问题(通过SQLSRV)
- 检索从MSSQL日期时间字段的日期通过SQL/PHP
- 通过查询字符串
- 发送日期到一个新的PHP页面尝试使用该日期在新SQL查询
我在这里遇到问题。
如果我使用的回声:
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)
如果我使用日期():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
如果我使用日期()与strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
我确定我完全错过了一些简单的东西。
编辑:我已经尝试了一些新的功能,我发现:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
:我一直在使用CAST试图
:
$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
EDIT 2
但是,这会失败,错误“varchar数据类型为datetime数据类型的转换导致超出范围的值”
谢谢。我不明白的是该字段已经是DATETIME字段,因此使用PHP更改其格式不应该很容易吗? – valoukh