select between two dates ms access using php

╄→尐↘猪︶ㄣ 提交于 2020-01-03 05:18:08

问题


i want to select data from ms access using php here is my code :

$dbdir = "D:\payroll2\ATT2000.MDB";
$conn = odbc_connect("DRIVER=Microsoft Access Driver (*.mdb);DBQ=$dbdir", 
  "administrator", 
  "");
$b = 4104;
$jo = date('n/j/Y h:i:s A',strtotime('2016-01-21 00:00:01'));
$ji = date('n/j/Y h:i:s A',strtotime('2016-01-21 23:59:59'));

$sql = "SELECT TOP 20 * from CHECKINOUT inner join USERINFO on CHECKINOUT.USERID = USERINFO.USERID where USERINFO.SSN = '$b'";
$rs = odbc_exec($conn,$sql);
odbc_fetch_row($rs, 0); 

while (odbc_fetch_row($rs)) { 

echo odbc_result($rs,"CHECKTIME"); print('<br>');

} 
 odbc_close($conn);
}

it works fine , but i want to select data between two dates so i add AND CHECKINOUT.CHECKTIME between '$jo' and '$ji' into the query like this :

$sql = "SELECT TOP 20 * from CHECKINOUT inner join USERINFO on CHECKINOUT.USERID = USERINFO.USERID where USERINFO.SSN = '$b' AND CHECKINOUT.CHECKTIME between '$jo' and '$ji'";

i dont know why it doest work, im sure the data is exist and my date format was persist like the ms access date format.

any help will appreciated, im sorry for my bad english..


回答1:


With MSAccess & MS SQL Server also I believe you need to use hash signs around the dates and you might also need to use CDate() to ensure the date is recognised as a date.

$sql = "SELECT TOP 20 * from CHECKINOUT 
    inner join USERINFO on CHECKINOUT.USERID = USERINFO.USERID 
    where USERINFO.SSN = '$b' AND CDate( CHECKINOUT.CHECKTIME ) between '#{$jo}#' and '#{$ji}#'";


来源:https://stackoverflow.com/questions/34916969/select-between-two-dates-ms-access-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!