In order to find the last week start date and end date you can follow up this code for doing it so. Show It works on all intervals to find the date interval.
If so you need to change it with the
If you want to find the last week range with the help of the functions you can preform like this. Function:
Usage:
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:
These will get you the dates of the previous Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Saturday. 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. Search within Codexpedia Custom Search Search the entire web 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));
|