我想获取开始日期和结束日期之间的日期,但是我面临的问题是,即使我的数据库中有日期,它也不会返回任何内容。
基本上,我想要任何具有
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) . ")";
最佳答案
也许更实际的解决方案是执行以下操作:
//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 . ")";
关于php - 和一起放大,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29432008/