开发者

PHP: Conversion failed when converting date and/or time from character string

开发者 https://www.devze.com 2023-01-21 20:02 出处:网络
I\'m having trouble with this query. I have 2 date coming from textboxes (don\'t worry I\'ve taken the necessary sql injection steps). They are used to query a MS SQL Server DATETIME field. I\'m getti

I'm having trouble with this query. I have 2 date coming from textboxes (don't worry I've taken the necessary sql injection steps). They are used to query a MS SQL Server DATETIME field. I'm getting this error:

Conversion failed when converting date and/or time from character string

Here my code:

 //formatting my strings
 $from = strtotime($from);
 $to = strtotime($to);

 //this is the where clause in the SQL statement
 "WHERE (tblBackupArchive.BackupDate BETWEEN '" . $from ."' AND '" . $t开发者_如何学Goo . "') "

What am I doing wrong?

Jonesy


strtotime converts the data string to a UNIX time stamp you should use

$from = date('Y-m-d H:i:s', strtotime($from));
$to   = date('Y-m-d H:i:s', strtotime($to));

Also bare in mind the 2K38 bug, so if you want to convert dates after 2038 with strtotime() you will get 0

Read more here:LINK


Have u checked that the strings are in the right format to be converted by strtotime? If you go to http://www.php.net/manual/en/datetime.formats.date.php it has a list of all the accepted formats

0

精彩评论

暂无评论...
验证码 换一张
取 消