Get last Sunday date in PHP
Solution 1
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'));
Solution 2
<?php
$date=date("Y-m-d");
echo "Current Date : ".$date."<br>";
echo "last Sunday : ".date('Y-m-d', strtotime($date.'last sunday'));
?>
Solution 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 1
st 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;
}
}
Solution 4
Use this code.
echo date('m/d/Y', strtotime('last Sunday'));
You can get last Sunday according to today.
Solution 5
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
Comments
-
J K almost 2 years
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.
-
J K about 11 yearsThat will return 2013-03-03. But what I need is 2013-02-24. Maybe I should say last last Sunday?
-
zerkms about 11 years@J K: the last sunday is March 3rd.
-
Sumit Bijvani about 11 years@zerkms I think he wants last last sunday
-
J K about 11 years@zerkms, Sorry, I think I should have said that I need to get last last Sunday. Sumit, -7? But, the script will run every day, and it should always return last last Sunday, until the following week. For example, today it should return 2013-02-24. Tomorrow, it should also return 2013-02-24, and the day after, it should also return 2013-02-24.... Then on 10 Mac 2013, it will start to return 2013-03-03.
-
Sumit Bijvani about 11 years@JK use this
echo date('Y-m-d',strtotime('last sunday -7 days'));
-
J K about 11 yearsWhat if when the script runs on 11 March 2013? Will it return 2013-03-03? If yes, then this is the solution. :)
-
Sumit Bijvani about 11 years@JK yes on 11 March 2013, it will return 2013-03-03 (last sunday - 10 March and last last sunday 03 March)
-
J K about 11 yearsYes, I think this will work. But, just to be sure,... let's wait until tomorrow when I run the script to see if it will still return 2013-02-24. Thanks so much.
-
Sumit Bijvani about 11 years@JK why you don't change your system date to tomorrow?, you can test it by changing your system date. and many thanks..
-
J K about 11 yearsBecause I do not have this script readily available on a local server. It's all live on a production server. We will see tomorrow or the day after. Thanks so much.
-
STT LCU about 11 yearsJ K, do you mean that you're developing and debugging on a live, production server? oh, my...