计算2000年01月01日起到指定日的天数


Posted in PHP onOctober 09, 2006

这是一个计算2000年01月01日起到指定日的天数的函数
<br>
(算头也算尾)日期格式为:YYYY-MM-DD
<br>

<?php
//    计算从2000年01月01日开始到某日的天数

function cal_start2end($end_day,$start_day)
{

$start_day=ereg_replace("-","",$start_day);
$end_day=ereg_replace("-","",$end_day);

if($end_day>=$start_day)
{    //    截止日期大于开始日期
    if(substr($end_day,0,4)==substr($start_day,0,4))
        {

if( is_int(substr($end_day,0,4)/4) )
    $leap_day=29;    //leap year
else
    $leap_day=28;

//            //同一年

        if(substr($end_day,4,2)==substr($start_day,4,2))
            {
//            the same month  
            $endday_from_startday=$end_day-$start_day+1;

            }
        else
            {
//             not same month

            switch (substr($end_day,4,2))
                {
                case "01":
                        $endday_from_newyear=substr($end_day,6,2)+1;
                        break;
                case "02":
                        $endday_from_newyear=substr($end_day,6,2)+31;
                        break;
                case "03":
                        $endday_from_newyear=substr($end_day,6,2)+$leap_day+31;
                        break;
                case "04":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2;
                        break;
                case "05":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2+30;
                        break;
                case "06":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*3+30;
                        break;
                case "07":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2+30*2;
                        break;
                case "08":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*3+30*2;
                        break;
                case "09":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*2;
                        break;
                case "10":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*3;
                        break;
                case "11":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*4;
                        break;
                case "12":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*5;
                        break;
                }
            $endday_from_startday=$endday_from_newyear;
            }

return ($endday_from_startday);

        }
    else
        {
//        不同年!

$differ_year=substr($end_day,0,4)-substr($start_day,0,4);

$how_int_4_floor=floor($differ_year/4)+1; // 向下取整
$how_int_4_ceil=ceil($differ_year/4)+1; // 向上取整

if($how_int_4_floor==$how_int_4_ceil)
    $how_leap_year=$how_int_4_floor-1;
else
    $how_leap_year=$how_int_4_floor;

$how_noleap_year=$differ_year-$how_leap_year;

$differ_year2day=$how_noleap_year*365+$how_leap_year*366;

if( is_int(substr($end_day,0,4)/4) )
    $leap_day=29;    //leap year
else
    $leap_day=28;

        if(substr($end_day,4,2)==substr($start_day,4,2))
            {
//            the same month  
            $endday_from_startday=$differ_year2day+ substr($end_day,4,2);

            }
        else
            {
//             not same month

            switch (substr($end_day,4,2))
                {
                case "01":
                        $endday_from_newyear=substr($end_day,6,2)+1;
                        break;
                case "02":
                        $endday_from_newyear=substr($end_day,6,2)+1+31;
                        break;
                case "03":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31;
                        break;
                case "04":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2;
                        break;
                case "05":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2+30;
                        break;
                case "06":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*3+30;
                        break;
                case "07":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*2+30*2;
                        break;
                case "08":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*3+30*2;
                        break;
                case "09":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*2;
                        break;
                case "10":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*3;
                        break;
                case "11":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*4;
                        break;
                case "12":
                        $endday_from_newyear=substr($end_day,6,2)+1+$leap_day+31*5+30*5;
                        break;
                } //End of switch

    $endday_from_startday=$endday_from_newyear+$differ_year2day;
    }    //End of  not same month  

return ($endday_from_startday);

        } //End of year
}        //End of  $end_day>=$start_day

} //End of function

$start_day="2000-01-01";

$end_day="2011-01-01";

echo "您输入的是:".$end_day."<br>\n";
$endday_from_startday=cal_start2end($end_day,$start_day);

echo "该日距离$start_day 共 ".$endday_from_startday." 天";

?>
<br><br><br><br>时间太少了,那位大虾可以修改为计算任意两日期之间的间隔最好!!!!! 

PHP 相关文章推荐
PHP语法速查表
Jan 02 PHP
php桌面中心(四) 数据显示
Mar 11 PHP
Smarty安装配置方法
Apr 10 PHP
php 全局变量范围分析
Aug 07 PHP
将一维或多维的数组连接成一个字符串的php代码
Aug 08 PHP
apache mysql php 源码编译使用方法
May 03 PHP
zf框架的校验器使用使用示例(自定义校验器和校验器链)
Mar 13 PHP
PHP数据库万能引擎类adodb配置使用以及实例集锦
Jun 12 PHP
PHP5.2中PDO的简单使用方法
Mar 25 PHP
php无法连接mysql数据库的正确解决方法
Jul 01 PHP
Laravel框架用户登陆身份验证实现方法详解
Sep 14 PHP
PHP实现获取毫秒时间戳的方法【使用microtime()函数】
Mar 01 PHP
文件上传程序的全部源码
Oct 09 #PHP
一个简单计数器的源代码
Oct 09 #PHP
一个用mysql_odbc和php写的serach数据库程序
Oct 09 #PHP
PHP脚本数据库功能详解(下)
Oct 09 #PHP
PHP脚本数据库功能详解(中)
Oct 09 #PHP
PHP脚本数据库功能详解(上)
Oct 09 #PHP
屏蔽浏览器缓存另类方法
Oct 09 #PHP
You might like
用Apache反向代理设置对外的WWW和文件服务器
2006/10/09 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
谈谈PHP中substr和substring的正确用法及相关参数的介绍
2015/12/16 PHP
php判断str字符串是否是xml格式数据的方法示例
2017/07/26 PHP
PHP实现的微信公众号扫码模拟登录功能示例
2019/05/30 PHP
同一个表单 根据要求递交到不同页面的实现方法小结
2009/08/05 Javascript
了解了这些才能开始发挥jQuery的威力
2013/10/10 Javascript
深入理解jquery中的事件与动画
2016/05/24 Javascript
Nodejs从有门道无门菜鸟起飞必看教程
2016/07/20 NodeJs
微信小程序 开发工具快捷键整理
2016/10/31 Javascript
JS+HTML5 FileReader对象用法示例
2017/04/07 Javascript
JavaWeb表单及时验证功能在输入后立即验证(含用户类型,性别,爱好...的验证)
2017/06/09 Javascript
bootstrap基本配置_动力节点Java学院整理
2017/07/14 Javascript
ionic3 懒加载
2017/08/16 Javascript
关于Webpack dev server热加载失败的解决方法
2018/02/22 Javascript
element-ui使用导航栏跳转路由的用法详解
2018/08/22 Javascript
this在vue和小程序中的使用详解
2019/01/28 Javascript
javascript实现评分功能
2020/06/24 Javascript
[00:52]DOTA2齐天大圣预告片
2016/08/13 DOTA
[00:18]天涯墨客三技能展示
2018/08/25 DOTA
在Python中封装GObject模块进行图形化程序编程的教程
2015/04/14 Python
python基于itchat实现微信群消息同步机器人
2017/02/27 Python
Numpy中转置transpose、T和swapaxes的实例讲解
2018/04/17 Python
在cmder下安装ipython以及环境的搭建
2018/10/19 Python
Python基于Tkinter模块实现的弹球小游戏
2018/12/27 Python
如何通过Python实现标签云算法
2019/07/02 Python
Pandas_cum累积计算和rolling滚动计算的用法详解
2019/07/04 Python
Python 之 Json序列化嵌套类方式
2020/02/27 Python
服务器端jupyter notebook映射到本地浏览器的操作
2020/04/14 Python
Selenium之模拟登录铁路12306的示例代码
2020/07/31 Python
运动会通讯稿500字
2014/02/20 职场文书
旅游活动总结
2014/08/27 职场文书
工伤事故赔偿协议书范文
2014/09/24 职场文书
淮阳太昊陵导游词
2015/02/10 职场文书
我的中国梦主题教育活动总结
2015/05/07 职场文书
Python获取百度热搜的完整代码
2021/04/07 Python