简体   繁体   中英

Get years and month between two dates

I am trying this code:

$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$interval = new DateInterval('P1Y'); // 1 Year interval

$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
    echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval

$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
    echo $dts->format( "m" );
}

I'm getting output like this:

year:2015
Month:08,09,...,01

In this output I'm not getting year:2016 and month:02. I want my output like this:

year:2015,2016
Month:08,09,...,01,02

How can I get this?

And if my end date is "2016-08-20"

than im getting year:2016 but not getting month :08

Note that my start date and end date is not fixed.

As mentioned before, the end date is not included

You may also modify the end date like that

$end = $end->modify( '+1 day +1 year' ); 

See php's DateTime modify for more info.

Update

If end year is more than 1 year bigger than the begin date then adding a year will indeed return a year more than what we want.

A solution is to check and add the year only if is needed like that:

if($begin > (new DateTime( $end_date))->modify('-1 year')) {
    $end->modify( '+1 year' );  
}
$end->modify('+1 day');

You can use the following code to achieve this:

<?php
$start    = (new DateTime('2015-12-02'))->modify('first day of this month');
$end      = (new DateTime('2016-05-06'))->modify('first day of this month');

//For Year
$interval = DateInterval::createFromDateString('1 year');
$period   = new DatePeriod($start, $interval, $end);
echo 'Year: ';

foreach ($period as $dt) 
{
    echo $dt->format("Y") . ",";
}

//For Month
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);
echo '<br/>Month: ';

foreach ($period as $dt) 
{
    echo $dt->format("m") . ",";
}
?>

Alternatively, you can display it nicely in Ym format by the following code:

$start    = (new DateTime('2015-12-02'))->modify('first day of this month');
$end      = (new DateTime('2016-05-06'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) 
{
    echo $dt->format("Y-m") . "<br>\n";
}

since the year 2016 and the month 02 is not completed you are not getting it. try this

$start_date = "2015-08-19";
$end_date = "2016-02-19";
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1Y1M'));
$interval = new DateInterval('P1Y'); // 1 Year interval

$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
    echo $dt->format( "Y" );
}
$intervals = new DateInterval('P1M'); // 1 month interval

$periods = new DatePeriod($begin, $intervals, $end);
foreach ( $periods as $dts ){
    echo $dts->format( "m" );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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