Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last Sunday date in 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.

like image 760
J K Avatar asked Mar 04 '13 07:03

J K


2 Answers

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')); 
like image 114
Sumit Bijvani Avatar answered Oct 13 '22 16:10

Sumit Bijvani


<?php
$date=date("Y-m-d");
echo "Current Date : ".$date."<br>";
echo "last Sunday : ".date('Y-m-d', strtotime($date.'last sunday'));
?> 
like image 35
aliasgar vanak Avatar answered Oct 13 '22 16:10

aliasgar vanak