zoukankan      html  css  js  c++  java
  • PHP获取自然周列表,周开始结束日期,月开始结束时间方法类

    自己写的几个获取 一年的自然周列表,一周的开始结束时间,一个月的开始结束时间的方法函数。请多大家多多批评指正!!!

    <?php
    
    function GetWeekList($year,$weekcurrent)
    {
        if ($year%4==0 && ($year%100!=0 || $year%400==0))
        {
            $days=366;    
        }
        else
        {
            $days=365;
        }
        $weeks=substr($days/7,0,2);//计算一年有多少个星期
    
        $weeklist=array();
        $wc=1;
        for($w=0;$w<$weekcurrent-1;$w++)
        {
            $WeekDate=GetWeekDate($year,$wc,'3');
            $startime=date("Y-m-d",$WeekDate[0]);
            $endtime=date("Y-m-d",$WeekDate[1]);
    
            $weeklist[$w]["id"]=$wc;
            $weeklist[$w]["name"]=$year." 年, 第 ".$wc." 周 ($startime - $endtime)";
            $wc++;
        }
    
        return $weeklist;
    }
    
    /////////////////////////////////计算一周的开始结束时间//////////////////////////////////////////
    function GetWeekDate($year,$week,$startcount)
    {
        $timestamp = mktime(0,0,0,1,1,$year);
        $dayofweek = date("w",$timestamp);
        $distance = ($week-1)*7-$dayofweek+1+$startcount;
        $passed_seconds = $distance * 86400;
        $timestamp += $passed_seconds;  
        $first_date_of_week = $timestamp; //date("Ymd",$timestamp);
        $distance = 7;
        $timestamp += $distance * 86400;
        $last_date_of_week = $timestamp; //date("Ymd",$timestamp);
    
        $startime=$first_date_of_week;
        $endtime=$last_date_of_week;
    
        $WeekDate=array($startime,$endtime);
    
        return $WeekDate;
    }
    
    function GetMonthList($year,$monthcurrent)
    {
        $mc=1;
        for($m=0;$m<12;$m++)
        {
            $monthlist[$m]["id"]=$mc;
            $monthlist[$m]["name"]=$year." 年,".$mc." 月 ";
            $mc++;
        }
    
        return $monthlist;
    }
    ///////////////////////////////计算一个月的开始结束时间//////////////////////////////////////////
    function GetMonthDate($year,$month) { if($month<10) { $month="0".$month; } $startime=$year."-".$month."-01"; //$months1=array(1,3,5,7,8,10,12); $months=array(4,6,9,11); if($month=="2") { if ($year%4==0 && ($year%100!=0 || $year%400==0)) { $endtime=$year."-".$month."-29 24:00:00"; } else { $endtime=$year."-".$month."-28 24:00:00"; } } else if(in_array($month,$months)) { $endtime=$year."-".$month."-30 24:00:00"; } else { $endtime=$year."-".$month."-31 24:00:00"; } $MonthDate=array($startime,$endtime); return $MonthDate; } ?>
  • 相关阅读:
    Maximum Depth of Binary Tree
    Single Number
    Merge Two Sorted Lists
    Remove Nth Node From End of List
    Remove Element
    Remove Duplicates from Sorted List
    Add Two Numbers
    编译视频直播点播平台EasyDSS数据排序使用Go 语言 slice 类型排序的实现介绍
    RTMP协议视频直播点播平台EasyDSS在Linux系统中以服务启动报错can’t evaluate field RootPath in type*struct排查
    【解决方案】5G时代RTMP推流服务器/互联网直播点播平台EasyDSS实现360°全景摄像机VR直播
  • 原文地址:https://www.cnblogs.com/betx/p/2521603.html
Copyright © 2011-2022 走看看