DateRange

DateRange

1 PHP[ | ]

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 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}