How does PHP List a date from the current date

Source: Internet
Author: User
Like what
$AA = "2013-12-25";
$BB =time ();
I would like to list 2013-12 distance from the current month

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

Now only the current month of the year is listed
for ($i = 1; $i <= date (' m '); $i + +) {
$dates [$i] = date (' y '. $i, Strtotime (Date ("y-m-d")));
}

Reply content:

Like what
$AA = "2013-12-25";
$BB =time ();
I would like to list 2013-12 distance from the current month

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

Now only the current month of the year is listed
for ($i = 1; $i <= date (' m '); $i + +) {
$dates [$i] = date (' y '. $i, Strtotime (Date ("y-m-d")));
}

Let's talk first.

    • This "need to know how many times the cycle is slightly difficult" problem while will make logic clear a lot
    • Strtotime Big Kill device
    • php4.3+
    • My code is short and clear, huh?
php//http://3v4l.org/vuef4function monthToToday($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);    }    //包含当前月份    $result[] = date($format, $current);    return $result;}print_r(monthToToday('2013-12-25', time()));die;

Subtract time stamps from both


  
    $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) { //1月,借年    $y=$a['year']-$b['year']-1;$m=$a['mon']-$b['mon']+12;}else {    if ($a['mon']>=$b['mon']+1) { //借月后,月相减为正        $y=$a['year']-$b['year'];$m=$a['mon']-$b['mon']-1;    }    else { //借月后,月相减为负,借年        $y=$a['year']-$b['year']-1;$m=$a['mon']-$b['mon']+12-1;    }}$datearr = array();$totalm = "";if( $y > 0 ){    $totalm = $y*12;//大于1年,转换成月份数}$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)));

Round code from: http://my.oschina.net/u/223350/blog/293687
Thanks to the original author, there are changes.

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

The result is that from the target time to today has been a few months a few days

Year X12+month = already over the 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);

Get the target time to the current month details

$aa = ' 2013-12-25 ';
$a = Strtotime ($AA);
$b = Array ();
while ($a<>
$b [] = date (' y-m ', $a);
$a = Strtotime (' +1 month ', $a);
}
Var_dump ($b);

After writing the discovery and mcfog thinking about the same

There seems to be something wrong with this question.
Time difference comes out, then, you one months is 28 days calculate, or 30 days or 31 days calculate?
Is the year 365 days or 366 days to count?

  • 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.