繁体   English   中英

php中已知年数和月数时,如何获取开始日期和结束日期

[英]How to get the start date and end date when the year number and month number are known in php

我有年份编号(例如:2018)和月份编号(例如:04)。 我想获取该月的开始日期和结束日期。 喜欢

2018-04-01 和 2018-04-30

试试这个

$year = 2018;
$month = 4;

$date_start = date('Y-m-d', strtotime(date($year.'-'.$month).' first day of this month'));
$date_end = date('Y-m-d', strtotime(date($year.'-'.$month).'last day of this month'));

echo $date_start . ' and ' . $date_end;
$months_array = $this->getting_particular_months_dates($start_day_of_year, $end_day_of_year);

                $arra1=array();
                foreach ($months_array as $K => $v)
                {
                    $year=explode( '-',$v['month'])[1];
                    $month=explode( '-',$v['month'])[0];

                    $arra1[]=$this->get_month_dates((int)$year,(int)$month);


                }
                return $arra1;

public function getting_particular_months_dates($year_start_date, $year_end_date)
    {
        $month_array = array();

        $date1 = $year_start_date;
        $date2 = $year_end_date;
        $output = [];
        $time = strtotime($date1);
        $last = date('m-Y', strtotime($date2));
        do {
            $month = date('m-Y', $time);
            $total = date('t', $time);

            $output[] = [
                'month' => $month,
                'total' => $total,
            ];

            $time = strtotime('+1 month', $time);
        } while ($month != $last);

        $month_array = $output;

        return $month_array;
    }

public function get_month_dates($year, $month)
    {
        $date_start = date('Y-m-d', strtotime(date($year . '-' . $month) . ' first day of this month'));
        $date_end = date('Y-m-d', strtotime(date($year . '-' . $month) . 'last day of this month'));

        $a = array('first_day' => $date_start, 'last_day' => $date_end);

        return $a;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM