一、日期相减:求相差年数、月数、日数
/** * 日期相减:相差年数、月数、日数 * * @param $sDate date <p>起始日期</p> * @param $eDate date <p>截止日期</p> * @param $sign string <p>年:y 月:m 日:d</p> * * @return int 相差数 */ function dateSubtraction($sDate, $eDate, $sign = "m") { $z = 0; if ($sDate > $eDate) { $temp = $sDate; $sDate = $eDate; $eDate = $temp; } $sDate_stamp = strtotime($sDate); $eDate_stamp = strtotime($eDate); list($date_1['y'], $date_1['m'], $date_1['d']) = explode("-", date('Y-m-d', $sDate_stamp)); list($date_2['y'], $date_2['m'], $date_2['d']) = explode("-", date('Y-m-d', $eDate_stamp)); switch ($sign) { case "y": $z = abs($date_1['y'] - $date_2['y']); break; case "m": $z = abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m']; break; case "d": $z = abs(($sDate_stamp - $eDate_stamp) / 86400); break; } return $z; }
二、自动生成连续日期:按年度、月度、日
/**
* 自动生成连续日期:按年度、月度、日
*
* @param $sDate date <p>起始日期</p>
* @param $eDate date <p>截止日期</p>
* @param $sign string <p>年:y 月:m 日:d</p>
*
* @return array 连续日期
*/
function dateContinuous($sDate, $eDate, $sign = "m")
{
$arrDate = Array();
if ($sDate > $eDate) { $temp = $sDate; $sDate = $eDate; $eDate = $temp; }
$n = dateSubtraction($sDate, $eDate, $sign);
switch ($sign) {
case "y":
for ($i = 0; $i <= $n; $i++) {
$arrDate[] = date("Y-m-d", strtotime("+" . $i . " years", strtotime($sDate)));
}
break;
case "m":
for ($i = 0; $i <= $n; $i++) {
$arrDate[] = date("Y-m-d", strtotime("+" . $i . " months", strtotime($sDate)));
}
break;
case "d":
for ($i = 0; $i <= $n; $i++) {
$arrDate[] = date("Y-m-d", strtotime("+" . $i . " days", strtotime($sDate)));
}
break;
}
return $arrDate;
}