用PHP获取上个星期天的约会

I am using a cron job to generate weekly reports on my database. Basically, the report generation script is in PHP. I scheduled a daily cron job.

My week for the reporting starts on Sunday.

I only want the report generation script to generate report, for the previous week from previous Sunday through to previous Monday.

Take for example.

Today is 4 March 2013. When the script runs, it should generate the report for 24 Feb 2013 to 3 March 2013. Tomorrow, when the script runs, it should also only run the report for 24 Feb 2013 to 3 March 2013.

How can I get last Sunday's date, automatically in my script?

Currently, I hard code using the following:

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

Any help is much appreciated. Thank you.

For Last Sunday

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

Edited Answer:

For Last Last Sunday

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

Use this code.

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

You can get last Sunday according to today.

try this

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

it will return 2013-02-24

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;
    }
}

Since this question is tagged also MySql, if you need to calculate the interval in a query you could use something like this:

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

Then to generate a report you can just use 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
<?php
$date=date("Y-m-d");
echo "Current Date : ".$date."<br>";
echo "last Sunday : ".date('Y-m-d', strtotime($date.'last sunday'));
?>