计算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 日期加减的类,很不错
Oct 10 PHP
php设计模式 Bridge (桥接模式)
Jun 26 PHP
php设计模式 Interpreter(解释器模式)
Jun 26 PHP
mcrypt启用 加密以及解密过程详细解析
Aug 07 PHP
PHP调用VC编写的COM组件实例
Mar 29 PHP
Thinkphp中import的几个用法详细介绍
Jul 02 PHP
PHP实现生成唯一会员卡号
Aug 24 PHP
Zend Framework实现Zend_View集成Smarty模板系统的方法
Mar 05 PHP
PHP调用API接口实现天气查询功能的示例
Sep 21 PHP
PHP通过GD库实现验证码功能示例
Feb 23 PHP
ThinkPHP5与单元测试PHPUnit使用详解
Feb 23 PHP
如何用RabbitMQ和Swoole实现一个异步任务系统
May 29 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
php出现web系统多域名登录失败的解决方法
2014/09/30 PHP
CodeIgniter配置之autoload.php自动加载用法分析
2016/01/20 PHP
PHP开发实现快递查询功能详解
2019/04/08 PHP
PHP远程连接oracle数据库操作实现方法图文详解
2019/04/11 PHP
jQuery 表单验证插件formValidation实现个性化错误提示
2009/06/23 Javascript
页面中body onload 和 window.onload 冲突的问题的解决
2009/07/01 Javascript
javascript 面向对象编程  function是方法(函数)
2009/09/17 Javascript
js之事件冒泡和事件捕获详细介绍
2013/10/28 Javascript
如何实现修改密码时密码框显示保存到cookie的密码
2013/12/10 Javascript
js实现点击链接后窗口缩小并居中的方法
2015/03/02 Javascript
JQuery实现动态适时改变字体颜色的方法
2015/03/10 Javascript
浅析jQuery事件之on()方法绑定多个选择器,多个事件
2016/04/27 Javascript
AngularJS解决ng界面长表达式(ui-set)的方法分析
2016/11/07 Javascript
微信小程序 列表的上拉加载和下拉刷新的实现
2017/04/01 Javascript
vue2 自定义动态组件所遇到的问题
2017/06/08 Javascript
angularjs实现的购物金额计算工具示例
2018/05/08 Javascript
详解小程序开发经验:多页面数据同步
2019/05/18 Javascript
node.js使用 http-proxy 创建代理服务器操作示例
2020/02/10 Javascript
把大数据数字口语化(python与js)两种实现
2013/02/21 Python
Python脚本实现代码行数统计代码分享
2015/03/10 Python
Python寻找路径和查找文件路径的示例
2019/07/10 Python
在Python中字典按值排序的实现方法
2020/11/12 Python
对CSS3选择器的研究(详解)
2016/09/16 HTML / CSS
css3编写浏览器背景渐变背景色的方法
2018/03/05 HTML / CSS
EVE LOM英国官网:全世界最好的洁面膏
2017/10/30 全球购物
Araks官网:纽约内衣品牌
2020/10/15 全球购物
七一表彰活动方案
2014/01/18 职场文书
《囚绿记》教学反思
2014/03/01 职场文书
战友聚会策划方案
2014/06/13 职场文书
2015年党员承诺书
2015/01/21 职场文书
护士求职自荐信
2015/03/25 职场文书
2016干部作风整顿心得体会
2016/01/22 职场文书
2016年综治和平安建设宣传月活动总结
2016/04/01 职场文书
MySQL之DML语言
2021/04/05 MySQL
mysql备份策略的实现(全量备份+增量备份)
2021/07/07 MySQL
Redis集群节点通信过程/原理流程分析
2022/03/18 Redis