- DateRange
1 PHP[ | ]
PHP
Copy
class DateRange extends ArrayIterator {
protected $oDate = null;
protected $oStartDate = null;
protected $oEndDate = null;
protected $oInterval = null;
function __construct( $strStart, $strEnd, $strInterval = 'auto' ) {
$this->oStartDate = new DateTime($strStart);
$this->oDate = clone $this->oStartDate;
$this->oEndDate = new DateTime($strEnd);
if( $strInterval == 'auto' ) $strInterval = ($strStart<$strEnd) ? '1 day' : '-1 day';
$this->oInterval = DateInterval::createFromDateString($strInterval);
}
function next() {
$this->oDate->add($this->oInterval);
return $this->oDate;
}
function current() {
return $this->oDate->format("Y-m-d");
}
function valid() {
if ($this->oStartDate > $this->oEndDate) return $this->oDate >= $this->oEndDate;
if ($this->oStartDate < $this->oEndDate) return $this->oDate <= $this->oEndDate;
return $this->oDate == $this->oEndDate;
}
}
$range = new DateRange("2000-01-01", "2000-01-03");
foreach($range as $date) echo $date.'<br>';
// 2000-01-01
// 2000-01-02
// 2000-01-03
$range = new DateRange("2000-01-03", "2000-01-01");
foreach($range as $date) echo $date.'<br>';
// 2000-01-03
// 2000-01-02
// 2000-01-01
$range = new DateRange("2000-01-01", "2000-03-01", "3 week");
foreach($range as $date) echo $date.'<br>';
// 2000-01-01
// 2000-01-22
// 2000-02-12
2 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.