与leap年的日期差 [英] Date difference with leap year

查看:138
本文介绍了与leap年的日期差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个不同的时间表,为期5周:

I have 5 different schedules for 5 weeks:


  1. 第一周=周一至周五(上午8点至下午5点)&&星期六和星期日的休息日

  2. 第二周=星期一至星期五(上午10点至下午6点)及周六和周日的休息日

  3. 第三周=周一至周五(上午11点至下午7点)&&星期六和星期日的休息日

  4. 第四周=星期一休息日&&周二至周六(上午10:30至下午6:30)及星期日休息日

  5. 第五周=星期一休息日&&周二至周六(上午8:30至下午5:30)及周日休息日

  1. first week = Monday to Friday (8am to 5pm) && Rest days on Saturday and Sunday
  2. second week = Monday to Friday (10am to 6pm) && Rest days on Saturday and Sunday
  3. third week = Monday to Friday (11am to 7pm) && Rest days on Saturday and Sunday
  4. fourth week = Monday Rest Day && Tuesday to Saturday (10:30 am to 6:30pm) && Sunday Rest Day
  5. fifth week = Monday Rest Day && Tuesday to Saturday (8:30 am to 5:30pm) && Sunday Rest Day

根据我的计算数组[0],[0],即第一周的星期一设置为4月25日, 2011。

Base on my calculation array [0],[0] which is Monday of first week is set to April 25, 2011.

我有这段代码可以计算输入日期和开始日期(即2011年4月25日)之间的差。

I have this code to compute the difference between input date and start date, which is April 25, 2011.

$tdays = floor((strtotime($date2) - strtotime($date1))/86400);

我现在可以计算从2011年4月到2012年2月的工作时间表。
但是,如果我输入的日期是2012年2月以后的日期,则由于leap年而导致输出错误。

I could now compute my work schedule starting April of 2011 up until February of 2012. However if I enter a date beyond February 2012, the output is wrong due to leap year. Is there a technique for this?

推荐答案

如果您能够使用php 5.3,则应该使用 date_diff()

If you are able to make use of php 5.3 you should use date_diff()

或尝试像这样的东西:

<?php
        function dateDifference($startDate, $endDate)
        {
            $startDate = strtotime($startDate);
            $endDate = strtotime($endDate);
            if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate)
                return false;

            $years = date('Y', $endDate) - date('Y', $startDate);

            $endMonth = date('m', $endDate);
            $startMonth = date('m', $startDate);

            // Calculate months
            $months = $endMonth - $startMonth;
            if ($months <= 0)  {
                $months += 12;
                $years--;
            }
            if ($years < 0)
                return false;

            // Calculate the days
                        $offsets = array();
                        if ($years > 0)
                            $offsets[] = $years . (($years == 1) ? ' year' : ' years');
                        if ($months > 0)
                            $offsets[] = $months . (($months == 1) ? ' month' : ' months');
                        $offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';

                        $days = $endDate - strtotime($offsets, $startDate);
                        $days = date('z', $days);   

            return array($years, $months, $days);
        }
?>

这篇关于与leap年的日期差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆