For和Implode Together

时间:2015-04-03 12:13:56

标签: php mysql database phpmyadmin

我希望在开始日期和结束日期之间得到日期,但我面临的问题是它没有返回任何内容,即使我的数据库中有日期。

基本上,我想要任何有

的东西
04/20/2015
04/21/2015
04/22/2015
04/23/2015
04/24/2015
04/25/2015
04/26/2015

这是我的代码:

//this gets days in between
$end = $enddate;//end date
$start = $gdate;//state date
$datediff = strtotime($end) - strtotime($start);
$datediff = floor($datediff/(60*60*24));
for($i = 0; $i < $datediff + 1; $i++){
   $cole = date("m/d/Y", strtotime($start . ' + ' . $i . 'day')). ",";
}
echo $cole;
//gets all this 04/20/2015,04/21/2015,04/22/2015,04/23/2015,04/24/2015,04/25/2015,04/26/2015,

//sql command
//$sql = "SELECT * FROM `timesheet` WHERE `date` LIKE (" . implode(',', $cole) . ")";

2 个答案:

答案 0 :(得分:0)

也许更实际的解决方案是做这样的事情:

//this gets days in between
$end = $enddate;//end date
$start = $gdate;//state date
$datediff = strtotime($end) - strtotime($start);
$datediff = floor($datediff/(60*60*24));
// Make $cole an array so that it can be imploded.
$cole = array();
for($i = 0; $i < $datediff + 1; $i++){
  // Instead of one long string, add each $datediff to the $cole array
  // and surround each piece of the array with quotes:
  $cole[] = "'" . date("m/d/Y", strtotime($start . ' + ' . $i . 'day')). "'";
}
// Now you implode $cole and not have to worry about wrapping each value in quotes:
$cole_imploded = implode(',', $cole);
// It's also probably more practical to use "IN" for your MySQL query, like so:
$sql = "SELECT * FROM `timesheet` WHERE `date` IN (" . $cole_imploded . ")";

答案 1 :(得分:0)

嗨试试这个简单的解决方案。

$sql = "SELECT * FROM `timesheet` WHERE `date` REGEXP '0?4\/2[0-6]\/2015$'";