PHP DateTime diff函数无法正常工作

时间:2015-11-02 10:08:33

标签: php datetime

在这种情况下。 我在unix时间戳中存储员工的休假信息。 我想做的是   - 在日历中显示休假日;   - 显示自上次休假以来经过了多长时间;

我试图获得的结果如下图所示: http://postimg.org/image/7tdw4hmwf/

问题是我的日历从31-12-1969开始并显示太多天。

以下是代码:

$vac_begin = '1412100000';
$vac_end = '1412877600';
// $vac_end = '1414087200';

$ve = new DateTime(date("d-m-Y", $vac_end));
$today = new DateTime(date('d-m-Y'));
$time_passed = $today->diff($ve)->format('%y years %m months');

echo 'Vacation start:'.date("d-m-Y", $vac_begin).'<br/>';
echo 'Vacation end:'.date("d-m-Y", $vac_end).'<br/>';
echo 'Time passed since last vacation: '.$time_passed.'<br/>';
//---------------------------------------
echo 'Days to show in calendar<br/>';

//Get monday of the week
$before = date("N", $vac_begin) - 1;
//Get sunday of the week
$after = 7 - date("N", $vac_end);

//Vacation days
$datediff = $vac_end - $vac_begin;
//Get number of remaining days to show in calendar
$remaining = 42 - (floor($datediff/(60*60*24)) + 1 + $before + $after);
//Allocate remaining days before and after vacation period
$floor = floor($remaining/7/2);
$round = round($remaining/7/2);

if($before > $after){
    $after += $round*7;
    $before += $floor*7;
} else{
    $after += $floor*7;
    $before += $round*7;
}

$format_sdate = date("d-m-Y", $vac_begin);
$format_edate = date("d-m-Y", $vac_end);

//Get first day to show in calendar
$cal_start_date = date("d-m-Y", strtotime($format_sdate.' -'.$before.' days'));
//Get last day to show in calendar
$cal_end_date = date("d-m-Y", strtotime($format_edate.' +'.$after.' days'));

$begin = new DateTime($cal_start_date);
$end = new DateTime($cal_end_date);
$end = $end->modify('+1 day');

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt){
    echo $dt->format("d").', ';
}

echo $cal_start_date;

0 个答案:

没有答案