如何获取php中已知年份编号和月份编号的开始日期和结束日期

时间:2018-04-23 09:47:23

标签: php date datetime

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

2018-04-01和2018-04-30

2 个答案:

答案 0 :(得分:2)

试试这个

C'

答案 1 :(得分:0)

$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;
    }