tags:

views:

40

answers:

2

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 '" . $to . "') "

What am I doing wrong?

Jonesy

A: 

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

infinity
Nearly there, except that `date()` needs a format string to work
Pekka
Yes you are very right, fixed
infinity
thanks the date function slipped my mind
iamjonesy
A: 

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

geoffs3310