PHP: Conversion failed when converting date and/or time from character string
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
精彩评论