English 中文(简体)
• 如何用 lo机把甲状腺素类的甲型六氯环己烷列入甲型六氯环己烷的样本?
原标题:How to list dates in yyyy_mm format with PHP using a loop?

What s the cleanest way to use a loop in PHP to list dates in the following way?

2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11

The key elements here:

  • Should be as simple as possible - I would prefer one for loop instead of two.
  • Should list this month s date as the first date, and should stop at a fixed point (2009-11)
  • Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)

Had to make a few tweaks to the solution:

    // Set timezone
    date_default_timezone_set( UTC );

    // Start date
    $date = date( Y ). - .date( m ). -01 ;
    // End date
    $end_date =  2009-1-1 ;

    while (strtotime($date) >= strtotime($end_date))
    {
        $date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
        echo substr($date,0,7);
        echo "
";
    }
最佳回答

Maybe this little code does the thing? : more complicated situations.

    <?php
        // Set timezone
        date_default_timezone_set( UTC );

        // Start date
        $date =  2009-12-06 ;
        // End date
        $end_date =  2020-12-31 ;

        while (strtotime($date) <= strtotime($end_date)) {
            echo "$date
";
            $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
        }

 ?>

The credit goes to: http://www.if-not-true-then-false.com/2009/php-loop-through-dates-from-date-to-date-with-strtotime-function/

问题回答

This is what im guessing your asking for cause it doesnt really make sense......

$startmonth = date("m");
$endmonth = 7;
$startyear = date("Y");
$endyear = 2012;

//First for loop to loop threw years
for($i=$startyear; $i<=$endyear; $i++, $startmonth=0) {
    //Second for loop to loop threw months
    for($o=$startmonth; $o<=12; $o++) {
        //If statement to check and throw stop when at limits
        if($i == $endyear && $o <= $endmonth)
            echo $i."_".$o."<br/>";
        else
            break;
    }
}

Will output:
2012_0
2012_1
2012_2
2012_3
2012_4
2012_5
2012_6
2012_7

PHP 5.3 introduces some great improvements to date/time processing in PHP. For example, the first day of, DateInterval and DatePeriod being used below.

$start    = new DateTime( first day of this month );
$end      = new DateTime( 2009-11-01 );
$interval = new DateInterval( P1M );
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
    echo $date->format( Y_m ) . PHP_EOL;
}




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签