介绍
老实说,我不确定你的意思,is it possible using function?
但我想你想要一个通用函数,但我认为一个简单的类也可以解决问题。
观察
它也应该只运行 2013 年 2 月 24 日至 2013 年 3 月 3 日的报告。
请注意,如果您正在绘制图形或将其用于分析,则 from Sunday 24 Feb 2013
to Sunday 3 March 2013
is 8 days
and more than 1 weeks
which 可能会导致数据。overlapping
我建议您将其限制 1 weeks
为Sunday, 24 Feb 2013
Saturday, 2 March 2013
这就是我的意思
// imagine today is Today is 4 March 2013
$today = DateTime::createFromFormat("d F Y", "4 March 2013");
$date = new PeroidDate();
vprintf("%s to %s", $date->getPeroid($today));
输出
Sunday, 24 February 2013 to Saturday, 02 March 2013
其他例子
示例 1
// Example
$date = new PeroidDate();
print_r($date->getPeroid());
输出
stdClass Object
(
[start] => Sunday, 31 March 2013
[end] => Saturday, 06 April 2013
)
//or
Sunday, 31 March 2013
示例 2
让我们想象一下,您想获得before valentine period
“2 月 14 日之前”的报告
print_r($date->getPeroid(new DateTime("2013-02-14")));
输出
stdClass Object
(
[start] => Sunday, 03 February 2013
[end] => Saturday, 09 February 2013
)
示例 3
让我们想象一下,您想要一个日期范围而不是单个范围,例如。你想要January 1
st和 the之间的日期Current Time
(可能你想将它用于选择框)然后你使用PeroidDate::getBetween
print_r($date->getBetween(new DateTime("2013-1-1"), new DateTime()));
输出
Array
(
[0] => stdClass Object
(
[start] => Sunday, 06 January 2013
[end] => Saturday, 12 January 2013
)
[1] => stdClass Object
(
[start] => Sunday, 13 January 2013
[end] => Saturday, 19 January 2013
)
[2] => stdClass Object
(
[start] => Sunday, 20 January 2013
[end] => Saturday, 26 January 2013
)
[3] => stdClass Object
(
[start] => Sunday, 27 January 2013
[end] => Saturday, 02 February 2013
)
[4] => stdClass Object
(
[start] => Sunday, 03 February 2013
[end] => Saturday, 09 February 2013
)
[5] => stdClass Object
(
[start] => Sunday, 10 February 2013
[end] => Saturday, 16 February 2013
)
[6] => stdClass Object
(
[start] => Sunday, 17 February 2013
[end] => Saturday, 23 February 2013
)
[7] => stdClass Object
(
[start] => Sunday, 24 February 2013
[end] => Saturday, 02 March 2013
)
[8] => stdClass Object
(
[start] => Sunday, 03 March 2013
[end] => Saturday, 09 March 2013
)
[9] => stdClass Object
(
[start] => Sunday, 10 March 2013
[end] => Saturday, 16 March 2013
)
[10] => stdClass Object
(
[start] => Sunday, 17 March 2013
[end] => Saturday, 23 March 2013
)
[11] => stdClass Object
(
[start] => Sunday, 24 March 2013
[end] => Saturday, 30 March 2013
)
[12] => stdClass Object
(
[start] => Sunday, 31 March 2013
[end] => Saturday, 06 April 2013
)
[13] => stdClass Object
(
[start] => Sunday, 07 April 2013
[end] => Saturday, 13 April 2013
)
)
使用的类
class PeroidDate {
private $format;
private $interval;
function __construct() {
$this->format = "l, d F Y";
$this->interval = new DateInterval('P7D');
}
function setFormat($format) {
$this->format = (string) $format;
}
function setInterval(DateInterval $format) {
$this->format = $format;
}
function getPeroid(\Datetime $date = null) {
$end = $date ? : new DateTime();
$end->modify("Last Sunday");
print_r($end->format($this->format));
$start = clone $end;
$start->sub($this->interval);
$end->modify("-1 day"); //
return (object) array(
"start" => $start->format($this->format),
"end" => $end->format($this->format)
);
}
function getBetween(Datetime $start = null, Datetime $end = null) {
if ($start > $end)
throw new InvalidArgumentException("`Start date` Must be greater than `End date`");
if ($start === null) {
$start = new DateTime();
$start->modify("First Sunday of January");
} else {
$start->modify("First Sunday");
}
if ($end === null) {
$end = new DateTime();
$end->modify("Last Sunday of December");
}
$range = array();
while ( $start < $end ) {
$r = new stdClass();
$r->start = $start->format($this->format);
$start->add($this->interval);
$tmp = clone $start;
$tmp->modify("-1 day");
$r->end = $tmp->format($this->format);
$range[] = $r;
}
return $range;
}
}