2010-10-26 152 views
0

我在查詢時遇到了問題。我有2個來自文本框的日期(不要擔心我已經採取了必要的SQL注入步驟)。它們用於查詢MS SQL Server DATETIME字段。我得到這個錯誤:PHP:從字符串轉換日期和/或時間時轉換失敗

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

這裏我的代碼:

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

我在做什麼錯?

Jonesy

+0

'$ from'和'$ to'包含什麼? – 2010-10-26 11:26:51

+0

他們是在日期格式 - dd/mm/yyyy – iamjonesy 2010-10-26 12:00:42

回答

0

的strtotime轉換數據串到UNIX時間戳 你應該使用

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

也生記2K38錯誤,所以如果你想在2038年之後到日期轉換用的strtotime( ),你會得到0

在這裏閱讀更多:LINK

+1

幾乎在那裏,除了'日期()'需要格式字符串才能工作 – 2010-10-26 11:26:07

+1

是的,你是非常正確的,修復 – infinity 2010-10-26 11:45:28

+0

感謝日期功能滑倒我的心靈 – iamjonesy 2010-10-26 12:02:05

相關問題