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

Introduction

To be honest am not sure what you mean by is it possible using function? but i guess you want a universal function but i think a simple class would also do the trick.

Observation

it should also only run the report for 24 Feb 2013 to 3 March 2013.

Please note that from Sunday 24 Feb 2013 to Sunday 3 March 2013 is 8 days and more than 1 weeks which may result to overlapping data if you are ploting graphic or using it for analytic. I would suggest you limit it to 1 weeks which is Sunday, 24 Feb 2013 to Saturday, 2 March 2013

Here is what i mean

// 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));

Output

 Sunday, 24 February 2013 to Saturday, 02 March 2013

Other Examples

Example 1

// Example
$date = new PeroidDate();
print_r($date->getPeroid());

Output

stdClass Object
(
    [start] => Sunday, 31 March 2013
    [end] => Saturday, 06 April 2013
)

//or
Sunday, 31 March 2013

Example 2

Lets Imagine you want to get report for before valentine period that is "before February 14th"

print_r($date->getPeroid(new DateTime("2013-02-14")));

Output

stdClass Object
(
    [start] => Sunday, 03 February 2013
    [end] => Saturday, 09 February 2013
)

Example 3

Lets Imagine you want a range of date instead of a single range,eg. You want the date between January 1st and the Current Time(Probably you want to use it for a select box) then you use PeroidDate::getBetween

print_r($date->getBetween(new DateTime("2013-1-1"), new DateTime()));

Output

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 Used

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

この質問には 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 に答える
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

このコードを使用します。

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

今日に従って先週の日曜日を取得できます。

于 2013-03-04T07:37:17.373 に答える