使用PHP循环日期

时间:2011-03-06 06:16:58

标签: php date loops

我试图用PHP循环日期。目前我的代码卡在一个重复110307的循环中。我需要日期格式为yymmdd。这是我试图使用的:

<?php
    $check_date = '100227';
    $end_date = '100324';

    while($check_date != $end_date){
        $check_date = date("ymd", strtotime("+1 day", strtotime($check_date)));
        echo $check_date . '<br>';  
    }
?>

4 个答案:

答案 0 :(得分:8)

尝试使用unix时间戳并每次添加86400。这比调用strtotime()要快。你可以lookup timestamp conversions online

<?php
    $check_date = 1267228800; // '2010-02-27';
    $end_date = 1269388800; // '2010-03-24';

    while($check_date != $end_date){
        $check_date += 86400;
        echo date("Ymd", $check_date) . '<br>';  
    }
?>

答案 1 :(得分:5)

strtotime将“100227”解释为今天10:02:27,而不是2010-02-27。所以在第一步之后,$check_date(今天)是“110307”。在所有后续步骤中,“110307”再次被解释为今天的时间,再次将$check_date作为“110307”。

迭代日期的一个巧妙方法是利用mktime标准化日期的能力,如下所示:

$date_arr = array(27,2,2010);
$end_date = "100324";
do {
    $check_date = gmdate('ymd', gmmktime(0,0,0,$date_arr[1],$date_arr[0]++,$date_arr[2]));
    echo $check_date."\n";
} while($end_date!=$check_date);

答案 2 :(得分:3)

以下是我喜欢的方式:

$startDate = new DateTime('20100227');
$endDate = new DateTime('20100324');

while ($startDate <= $endDate) {
  // your code here
  ...
  // go to the next day
  $startDate->add(new DateInterval('P1D'));
}

我个人认为这个更干净,而且不必硬编码像84600这样的值。

答案 3 :(得分:2)

以下是我使用的代码的一部分,可能会有所改进,取决于您使用的PHP版本。

//usage
$Iterator=class Dates_DateIterator::factory('Daily',
                                           new Datetime('20100227'),
                                           new Datetime('20100324'));

foreach($Iterator as $i=>$day){
    var_dump($i);
    var_dump($day);
}


//code lib
abstract class Dates_DateIterator implements Iterator
{
    /**
     * Factory method, saves some code, also enable me to put everything in the same class 
     * as we use Autoload to load classes.
     */
    static public function factory($cycle,DateTime $DateI,DateTime $DateII){
        switch($cycle){
            case 'Daily':
                return new DaysIterator($DateI,$DateII);
            case 'Weekly':
                return new WeeksIterator($DateI,$DateII);
            case 'Monthly':
                return new MonthsIterator($DateI,$DateII);
            case 'Yearly':
                return new YearsIterator($DateI,$DateII);
            default:
                throw(new Exception('No valid cycle was chosen to iterate over'));
        }
    }
    /**
     * @var DateTime represents the start range.
     */
    public $FromDate;
    /**
     * @var DateTime represents the end range.
     */
    public $ToDate;
    /**
     * @var DateTime Current Date.
     */
    protected $CurrentDate;

    public function __construct(DateTime $DateI,DateTime $DateII)
    {
        if($DateII->format('U') > $DateI->format('U'))
        {
            $this->FromDate=$DateI;
            $this->ToDate=$DateII;
            $this->CurrentDate=$DateI;
        }
        else
        {
            $this->FromDate=$DateII;
            $this->ToDate=$DateI;
            $this->CurrentDate=$DateII;
        }
    }//EOF constructor

    /**
     * @return DateTime
     */
    public function getClonedCurrent(){
        return clone($this->CurrentDate);   
    }

    public function current()
    {
        return $this->CurrentDate;
    }//EOF current

    public function currentDate()
    {
        return $this->CurrentDate->format('Ymd');
    }//EOF current

    public function rewind()
    {
        $this->CurrentDate=$this->FromDate;
    }//EOF rewind

    public function valid()
    {
        //Kill hours/minutes/seconds. If we are to add hours and minutes iterators, we will need to rethink this.
        return (floor($this->CurrentDate->format('U')/(3600*24)) <= floor($this->ToDate->format('U')/(3600*24)));
    }//EOF valid    
}//EOF CLASS  DateIterator









class DaysIterator extends SiTEL_Dates_DateIterator
{
    public function __construct(DateTime $DateI,DateTime $DateII)
    {
        parent::__construct($DateI,$DateII);
    }//EOF constructor

    public function next()
    {
        $this->CurrentDate->modify('+1 day');
    }//EOF next

    public function key()
    {
        return $this->CurrentDate->format('d');
    }//EOF key

}//EOD CLASS DaysIterator