11

我正在使用cron 作业在我的数据库上生成每周报告。基本上,报告生成脚本是用 PHP 编写的。我安排了一个每日 cron 作业。

我的报告周从周日开始。

我只希望报告生成脚本生成报告,从上周日到上周一的前一周。

举个例子。

今天是 2013 年 3 月 4 日。当脚本运行时,它应该生成 2013 年 2 月 24 日到 2013 年 3 月 3 日的报告。明天,当脚本运行时,它也应该只运行 2013 年 2 月 24 日到 2013 年 3 月 3 日的报告。

如何在我的脚本中自动获取上周日的日期?

目前,我使用以下代码进行硬编码:

$startDate = strtotime("13 January 2013");

$strStartDate = date ("Y-m-d 00:00:00", $startDate);
$strEndDate = date ("Y-m-d 23:59:00", $startDate + (6*24*60*60));

任何帮助深表感谢。谢谢你。

4

6 回答 6

26

上周日

echo date('Y-m-d',strtotime('last sunday'));

编辑答案:

最后上周日

echo date('Y-m-d',strtotime('last sunday -7 days')); 
于 2013-03-04T07:37:39.047 回答
4
<?php
$date=date("Y-m-d");
echo "Current Date : ".$date."<br>";
echo "last Sunday : ".date('Y-m-d', strtotime($date.'last sunday'));
?> 
于 2013-06-13T05:53:51.147 回答
3

介绍

老实说,我不确定你的意思,is it possible using function?但我想你想要一个通用函数,但我认为一个简单的类也可以解决问题。

观察

它也应该只运行 2013 年 2 月 24 日至 2013 年 3 月 3 日的报告。

请注意,如果您正在绘制图形或将其用于分析,则 from Sunday 24 Feb 2013to Sunday 3 March 2013is 8 daysand more than 1 weekswhich 可能会导致数据。overlapping我建议您将其限制 1 weeksSunday, 24 Feb 2013Saturday, 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 1st和 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;
    }
}
于 2013-04-12T15:00:53.667 回答
1

使用此代码。

echo date('m/d/Y', strtotime('last Sunday'));

你可以根据今天得到上周日。

于 2013-03-04T07:37:17.373 回答
1

试试这个

$sun =  date('Y-m-d',strtotime('last sunday'));
echo date('Y-m-d', strtotime('last Sunday', strtotime($sun)));

它会回来2013-02-24

于 2013-03-04T07:51:42.600 回答
1

由于这个问题也被标记为 MySql,如果您需要计算查询中的间隔,您可以使用以下内容:

SELECT
  '2013-03-04' - INTERVAL (WEEKDAY('2013-03-04')+1) % 7 DAY - INTERVAL 1 WEEK start_date,
  '2013-03-04' - INTERVAL (WEEKDAY('2013-03-04')+1) % 7 DAY end_date

然后要生成报告,您可以使用 CURDATE():

SELECT *
FROM   your_tables
WHERE  ... AND
       report_date BETWEEN
         CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) % 7 DAY - INTERVAL 1 WEEK
         AND CURDATE() - INTERVAL (WEEKDAY(CURDATE())+1) % 7 DAY
于 2013-04-13T07:59:27.770 回答