How does PHP list a date from the current date?

Source: Internet
Author: User
For example, $ aa & quot; 2013-12-25 & quot; $ bbtime (); I want to list the month array (12) {[1] & amp; gt; string (6) & quot; 2013-12 & quot; [2] & amp; gt; string (6) & quot; 2014-1 & quot; [3] & amp; gt; string (6) & quot; 2014-2 & quot; [4] & amp; gt; string (6) & quot; 2014-3 & quot; [5] For example
$ Aa = "2013-12-25 ";
$ Bb = time ();
I want to list the months between and 12

Array (12 ){
[1] = & gt; string (6) "2013-12"
[2] => string (6) "2014-1"
[3] => string (6) "2014-2"
[4] => string (6) "2014-3"
[5] => string (6) "2014 -...."
}

Currently, only the current month of the current year can be listed.
For ($ I = 1; $ I <= date ('M'); $ I ++ ){
$ Dates [$ I] = date ('Y-'. $ I, strtotime (date ("Y-m-d ")));
}

Reply content:

For example
$ Aa = "2013-12-25 ";
$ Bb = time ();
I want to list the months between and 12

Array (12 ){
[1] = & gt; string (6) "2013-12"
[2] => string (6) "2014-1"
[3] => string (6) "2014-2"
[4] => string (6) "2014-3"
[5] => string (6) "2014 -...."
}

Currently, only the current month of the current year can be listed.
For ($ I = 1; $ I <= date ('M'); $ I ++ ){
$ Dates [$ I] = date ('Y-'. $ I, strtotime (date ("Y-m-d ")));
}

Let's start with your thoughts.

  • This problem is used to "know how many cycles are a little difficult ".whileWill make the logic clearer
  • Strtotime kill
  • PHP4.3 +
  • // My code is short and clear, hum
php// Http://3v4l.org/vuef4function monthtoday ($ past, $ now, $ format = 'Y-m') {$ current = strtotime (date ('y m-1 ', strtotime ($ past); $ result = array (); while ($ current <$ now) {$ result [] = date ($ format, $ current ); $ current = strtotime ('+ 1 month', $ current);} // contains the current month $ result [] = date ($ format, $ current); return $ result ;} print_r (monthtoday ('2017-12-25 ', time (); die;

Timestamp offset


   $monthDiffed) {        $m = $month;        $month = $monthDiffed;        $monthDiffed = $m;    }    $monthDiffed = $monthDiffed->modify("last day of -1 month");    $month = $month->modify("first day of +1 month");    $result = array();    while($month <= $monthDiffed) {        $result[] = $month->format( $format );        $month = $month->modify("+1 month");    }    return $result;}print_r( diffMonth("2014-6") );print_r( diffMonth("2014-6", "Y/m", "2013-6") );

$ Aa = "2003-02-25"; $ bb = date ("Y-m-d", time (); function datediffage ($ before, $ after) {$ B = getdate ($ before); $ a = getdate ($ after); $ y = $ m = 0; if ($ a ['mon'] = 1) {// for June 1, January, borrow year $ y = $ a ['Year']-$ B ['Year']-1; $ m = $ a ['mon']-$ B ['mon'] + 12 ;} else {if ($ a ['mon']> = $ B ['mon'] + 1) {// after the month, the monthly offset is $ y = $ a ['Year']-$ B ['Year']; $ m = $ a ['mon']-$ B ['mon']-1;} else {// after the month is borrowed, the month is reduced to negative, borrow year $ y = $ a ['Year']-$ B ['Year']-1; $ m = $ a ['mon']-$ B ['mon'] + 12-1 ;}$ datearr = array (); $ totalm = ""; if ($ y> 0) {$ totalm = $ y * 12; // if the value is greater than one year, it is converted to the monthly number of copies} $ totalm + = $ m; for ($ I = 0; $ I <$ totalm; $ I ++) {array_push ($ datearr, date ("Y-m", mktime (0, 0, 0, $ B ['mon'] + $ I, $ d, $ B ['Year']);} return $ datearr;

}

Print_r (datediffage (strtotime ($ aa), strtotime ($ bb )));

Circle code from: http://my.oschina.net/u/223350/blog/293687
Thanks to the original author for the modification.

echo (new DateTime('2013-12'))->diff(new DateTime(date('Y-m')))->format('%y year %m month %d days');

The result is that it has been several months and days since the target time.

Year x12 + month = past few months

$date = new DateTime('2013-12');$now = date('Ym');$months = array();while ($date->format('Ym')<=$now) {    array_push($months, $date->format('Y-m'));    $date->modify('+1 month');}print_r($months);

Obtain the month details from the target time to the present.

$ Aa = '2017-12-25 ';
$ A = strtotime ($ aa );
$ B = array ();
While ($ $ B [] = date ('Y-m', $ );
$ A = strtotime ('+ 1 month', $ );
}
Var_dump ($ B );

After writing, I found that the idea is similar to that of mcfog.

This problem seems to be all wrong,
If the time difference comes out, do you calculate for a month based on 28 days, or 30 days or 31 days?
Is the year calculated by 365 days or 366 days?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.