Get date last week php

In order to find the last week start date and end date you can follow up this code for doing it so.

It works on all intervals to find the date interval.

$Current = Date('N');
$DaysToSunday = 7 - $Current;
$DaysFromMonday = $Current - 1;
$Sunday = Date('d/m/y', strtotime("+ {$DaysToSunday} Days"));
$Monday = Date('d/m/y', strtotime("- {$DaysFromMonday} Days"));

If so you need to change it with the datatime() you can perform this function.

$date = new DateTime();
$weekday = $date->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$date->modify("-$diff day");
echo $date->format('Y-m-d') . ' - ';
$date->modify('+6 day');
echo $date->format('Y-m-d');

Using Functions:

If you want to find the last week range with the help of the functions you can preform like this.

Function:

// returns last week's range
function last_week_range($date) {
$ts = strtotime("$date - 7 days");
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
return array(
      date('Y-m-d', $start),
      date('Y-m-d', strtotime('next saturday', $start))
);
}

Usage:

$today=date();
print_r(last_week_range($today));

All the above functions that has been given will return the last week range irrespective of the start day of the week..

This is a PHP guide on how to get a date that was 7 days ago. Most of these examples are pretty easy to change, so you should be able to modify them to suit your own needs.

To get last week’s date, we can use PHP’s date function and the strtotime function:

//One week ago in a YYYY-MM-DD format.
$lastWeek = date("Y-m-d", strtotime("-7 days"));
echo $lastWeek, '<br>';

The above code outputted “2019-04-11” on the 18th of April, 2019.

If you want last week’s date to be displayed in a more human-friendly format, you can use the following example:

$lastWeek = date("l, jS F, Y", strtotime("-7 days"));
echo $lastWeek;

This will output a date such as “Thursday, 11th April, 2019”.

You can also get last week’s date by providing the strtotime function with “-1 week”:

strtotime("-1 week")

Note that strtotime returns a UNIX timestamp. i.e. If you want the UNIX timestamp of last week’s date, you can just do the following:

//Get unix timestamp of last week.
echo strtotime("-1 week");

This will output a timestamp such as “1554973963”.

Getting last week’s date with the DateTime object.

If you’re using PHP version 5.2 or above, you can utilize the Datetime functions:

//Instantiate the DateTime object.
$dateTime = new DateTime();

//Set the date to -7 days using the modify function.
$dateTime->modify('-7 day');

//Print out the date in a YYYY-MM-DD format.
echo $dateTime->format("Y-m-d");

In the example above, we used DateTime’s modify method to set the date back -7 days.

But what if you want to go one week back from a given date?

//Set a specific date.
$dateTime = new DateTime('2019-01-12');

//Use modify method to go back -7 days from that date.
$dateTime->modify('-7 day');

//Print out the date in a YYYY-MM-DD format.
echo $dateTime->format("Y-m-d");

Here, we manually set the date to “2019-01-12” before subtracting 7 days. The output of this particular example is “2019-01-05” because that was a week before “2019-01-12”.

Hopefully, you found this tutorial useful!

Related:

  • Subtracting days from dates using PHP.
  • Getting last week’s date using JavaScript.

These will get you the dates of the previous Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Saturday.

date("Y-m-d", strtotime("previous monday"));
date("Y-m-d", strtotime("previous tuesday"));
date("Y-m-d", strtotime("previous wednesday"));
date("Y-m-d", strtotime("previous thursday"));
date("Y-m-d", strtotime("previous friday"));
date("Y-m-d", strtotime("previous saturday"));
date("Y-m-d", strtotime("previous sunday"));

If the current day is tuesday, and you want to get the monday of last week, then the previous monday will not get the monday you want, it will get the date of the monday just the day before tuesday. To get the dates from Monday to Friday of the last week, we need to calculate the difference between the previous monday from the strtotime function and the current date, if the difference is less than 7 that means the monday is within the current week. If it is the monday of the current week, then we can apply the previous monday one more time to get the monday of last week. The function below returns an array of the dates from Monday to Sunday of the last week.

function getLastWeekDates()
{
	$lastWeek = array();

	$prevMon = abs(strtotime("previous monday"));
	$currentDate = abs(strtotime("today"));
	$seconds = 86400; //86400 seconds in a day

	$dayDiff = ceil( ($currentDate-$prevMon)/$seconds ); 

	if( $dayDiff < 7 )
	{
		$dayDiff += 1; //if it's monday the difference will be 0, thus add 1 to it
		$prevMon = strtotime( "previous monday", strtotime("-$dayDiff day") );
	}

	$prevMon = date("Y-m-d",$prevMon);

	// create the dates from Monday to Sunday
	for($i=0; $i<7; $i++)
	{
		$d = date("Y-m-d", strtotime( $prevMon." + $i day") );
		$lastWeek[]=$d;
	}

	return $lastWeek;
}

Search within Codexpedia

Get date last week php

Custom Search

Search the entire web

Get date last week php

Custom Search

How can I get last Monday date in PHP?

Just use the date relative format: $date = new DateTime("last monday");

How do you the Get This week date and range in PHP?

php get this week date range.
$monday = strtotime('last monday', strtotime('tomorrow'));.
$sunday = strtotime('+6 days', $monday);.
echo "<P>". date('d-M-Y', $monday) . " to " . date('d-M-Y', $sunday) . "</ P>";.

How do I get the start of the week in PHP?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How to format date in PHP?

Change YYYY-MM-DD to DD-MM-YYYY In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate));