嗨,对于stackoverflow中的所有人,我想请求你们的帮助。我一直坚持我正在做的事情,我想计算我的查询中存在的天数,但我不知道如何。有人可以帮我吗?非常感谢一种形式的帮助。
以下是我的评论代码:
//Used for getting the values
$mfrom = $this->input->get('mfroma'); //From: Month
$yfrom = $this->input->get('yfroma'); //From: Year
$yto = $this->input->get('ytoa'); //To : Year
$n_of_months = $this->input->get('n_of_monthsa'); // Month or year Range
$cmonth = $mfrom; //From: Month
$cyear = $yfrom; //From: Year
$clastyear = $yto; //To: Year
$sSQLSelect = "";
$sSQLSelect1 = "";
for($i=0;$i<=$n_of_months;$i++)
{
$curdate = $cmonth." ".$cyear;
$sSQLSelect = $sSQLSelect."SUM(IF(DATE_FORMAT(date_d, '%c %Y') = '$curdate' ,
IF(DP.hrsdiff <= 4, 0.5, 1), 0)) AS 'month$cmonth$cyear', ";
$sSQLSelect1 = $sSQLSelect1."month$cmonth$cyear + ";
if($cmonth+1 > 12) {
$cyear++;
$cmonth = 1;
} else
$cmonth++;
}
date_default_timezone_set('Asia/Manila');
$current_date = date('Y-m',time());
$d = array();
$i = 0;
$sSQL = "SELECT TS.id AS TSid, employeeid, `date` AS date_d, TS.TI1 AS TI1, TS.TO1 AS TO1,
CONCAT(E.lastName,', ',E.firstName,' ',E.middleInitial,'.') AS 'fullname',
(TIME_TO_SEC(CAST(TS.`TI1` AS time))/3600) AS 'timeinint',
(TIME_TO_SEC(CAST(TS.`TO1` AS time))/3600) AS 'timeoutint',
E.startDate, C.companyName, B.branchName,
R.mon, R.tue, R.wed, R.thu, R.fri, R.sat, R.sun
FROM timeschedule AS TS
LEFT JOIN employee AS E ON TS.employeeid = E.id
LEFT JOIN company AS C ON E.companyid = C.id
LEFT JOIN branch AS B ON E.branchid = B.id
LEFT JOIN restday AS R ON R.id = E.restDayid
WHERE TS.`show` = '1' $namecondition $companynamecondition $branchnamecondition
GROUP BY employeeid
ORDER BY fullname ASC";
$result = $this->db->query($sSQL);
$data = array();
foreach($result->result() as $row)
{
$rowemployeedetails = $row;
$fullname = $row->fullname;
$startdate = $row->startDate;
$st_month = (int)(date("m",strtotime($startdate)));
$st_day = (int)(date("d",strtotime($startdate)));
$st_year = (int)(date("Y",strtotime($startdate)));
$data['data'][$i][] = array($row->fullname);
$data['data'][$i][] = array($row->companyName);
$data['data'][$i][] = array($row->branchName);
// #$cmonth = start_month
// #$cyear = start_year
// #number months in between
$cmonth = $mfrom;
$cyear = $yfrom;
for($j=0; $j<= $n_of_months ;$j++){
if((date('Y-m', strtotime($startdate)) <= date('Y-m', strtotime($cyear.'-'.$cmonth)))&&($current_date >= date('Y-m', strtotime($cyear.'-'.$cmonth))))
{
$d[$j] = ((date('Y-m', strtotime($cyear.'-'.$cmonth)) == date('Y-m',time()))?date('d', time()):cal_days_in_month(CAL_GREGORIAN, $cmonth, $cyear));
}
else
{
$d[$j]=0;
}
if($cmonth === 12){
$cyear++;
$cmonth = 1;
}
else{
$cmonth++;
}
}
// #Result: d[] = number of days in month
// #$cyear = start year filter
// #$clastyear = last year filter
// #$cmonth = start_month
$cyear = $yfrom;
$clastyear = $yto;
}
$result->free_result();
return json_encode($data);
}
答案 0 :(得分:0)
如果您有两个日期并想知道从一个日期到另一个日期的天数,一个简单的解决方案就是使用DateTime::diff,如下所示:
<?php
$from = new DateTime('2014-03-28');
$to = new DateTime('2014-03-30');
$interval = $from->diff($to);
echo $interval->format('%R%a'); // +2