Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I get last week' date range in php?

Tags:

date

php

how can I get last week' date range in php ?

see my codes bellow:

<?php      function get_last_week_dates(){         // how can i get the date range last week ?         // ex: today is 2014-2-8         // the week date range of last week should be '2014-1-26 ~ 2014-2-1'     } ?> 
like image 853
mingfish_004 Avatar asked Feb 08 '14 08:02

mingfish_004


People also ask

How can I get last week Sunday date in PHP?

Use this code. echo date('m/d/Y', strtotime('last Sunday'));

How can I get last Monday date in PHP?

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

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


Video Answer


1 Answers

You can use strtotime()

$previous_week = strtotime("-1 week +1 day");  $start_week = strtotime("last sunday midnight",$previous_week); $end_week = strtotime("next saturday",$start_week);  $start_week = date("Y-m-d",$start_week); $end_week = date("Y-m-d",$end_week);  echo $start_week.' '.$end_week ; 

UPDATE

Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.

$previous_week = strtotime("-1 week +1 day"); 

In addition if we need to find the current week and next week date range we can do as

Current week -

$d = strtotime("today"); $start_week = strtotime("last sunday midnight",$d); $end_week = strtotime("next saturday",$d); $start = date("Y-m-d",$start_week);  $end = date("Y-m-d",$end_week);   

Next Week -

$d = strtotime("+1 week -1 day"); $start_week = strtotime("last sunday midnight",$d); $end_week = strtotime("next saturday",$d); $start = date("Y-m-d",$start_week);  $end = date("Y-m-d",$end_week);  
like image 187
Abhik Chakraborty Avatar answered Oct 05 '22 13:10

Abhik Chakraborty