donglu5728 2015-11-02 10:08
浏览 43

PHP DateTime diff函数无法正常工作

Here's the case. I store vacation information of employees in mysql in unix timestamp. What I want to do is to - show the vacation days in a calendar; - show how much time passed since last vacation;

The result I'm trying to get is like in this picture: http://postimg.org/image/7tdw4hmwf/

The problem is my calendar is starting from 31-12-1969 and showing too many days.

Here's the code:

$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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口