开始日期= 2015-01-01 结束日期= 2015-02-01
date = 2015-01-15
我想要两个像这样的间隔
间隔1 开始日期= 2015-01-01 结束日期= 2015-01-15
间隔2 开始日期= 2015-01-16 结束日期= 2015-02-01
我的代码看起来像这样
<?php
$fromdate = '2015-12-01';
$todate ='2015-01-01';
if ($fromdate < '2015-12-18' && $todate > '2015-12-18')
{
// here i have to split interval into two intervals and get data from both tables old_orders and new_orders
$fromdate1 = '2015-12-01'; $todate1 = '2015-12-15';
$fromdate2 = '2015-12-16'; $todate2 = '2015-01-01';
$query1 = "SELECT * FROM new_orders WHERE date BETWEEN '".$fromdate1."' AND '".$todate1."'";
$query2 = "SELECT * FROM new_orders WHERE date BETWEEN '".$fromdate2."' AND '".$todate2."'";
}
else if ($fromdate > '2015-12-18')
{
$query1 = "SELECT * FROM new_orders WHERE date BETWEEN '".$fromdate."' AND '".$todate."'";
}
else if ($todate < '2015-12-18')
{
$query1 = "SELECT * FROM old_orders WHERE date BETWEEN '".$fromdate."'" AND '".$todate."'";
}
答案 0 :(得分:1)
试试这个:
<?php
$startDate = '2015-01-01';
$endDate = '2015-02-01';
$date = '2015-01-15';
$intervals = array();
$intervals[] = array('startDate'=>$startDate, 'endDate'=>$date);
$intervals[] = array('startDate'=>date('Y-m-d', strtotime($date . ' +1 day')), 'endDate'=>$endDate);
在$intervals
数组中,您的结构如下:
Array
(
[0] => Array
(
[startDate] => 2015-01-01
[endDate] => 2015-01-15
)
[1] => Array
(
[startDate] => 2015-01-16
[endDate] => 2015-02-01
)
)
答案 1 :(得分:0)
如果我理解你,你就需要这样的东西:
function isDateBetweenDates ($date, $startDate, $endDate) {
$strDate = strtotime($date);
$strStartDate = strtotime($startDate);
$strEndDate = strtotime($endDate);
return ($strDate >= $strStartDate && $strDate <= $strEndDate);
}
$date = "2015-01-15";
$startDate = "2015-01-01";
$endDate = "2015-02-01";
class Interval {}
$interval = new Interval();
if (isDateBetweenDates($date, $startDate, $endDate)) {
$interval->firstPart = array(
"start" => $startDate,
"end" => $date
);
$newDate = new DateTime($date);
$newDate->modify("1 day");
$interval->secondPart = array(
"start" => $newDate->format("Y-m-d"),
"end" => $endDate
);
}
最好的问候。