Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,当年数和月数已知时,如何获取开始日期和结束日期_Php_Date_Datetime - Fatal编程技术网

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

在php中,当年数和月数已知时,如何获取开始日期和结束日期,php,date,datetime,Php,Date,Datetime,我有年份号(ex:2018)和月份号(ex: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).'

我有年份号(ex:2018)和月份号(ex: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;

嗯,开始日期是
年-月-01
,结束日期是
年-下个月-01-1
。可能的重复我想这不是他要找的。他想得到一个月的第一天和最后一天再次检查这个问题。。。他说他需要这样:
2018-04-01和2018-04-30
你的答案将输出相同内容的2倍,它将响应
2018-04-01和2018-04-30
好的,你明白了,我不知道,只是尝试了一下,效果不错
$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;
    }