1

Can anyone help me how to find last week starting date or ending date using currentdate

Suppose i am having today date is :-

enter code here
date('y-m-d') = 2017-01-31 today is Tuesday

So i want last week starting date and ending date output like

enter code here

last Week starting date:- 2017-01-23
last week ending date :- 2017-01-29

Can anyone hlp me how to do that

kunal
  • 4,122
  • 12
  • 40
  • 75

3 Answers3

2

first get the week number than get previous week and get its date.

   $ddate = "2017-1-31";
$date = new DateTime($ddate);
$week = $date->format("W");
$year = $date->format("o");
var_dump($week);
function etStartAndEndDate($week, $year) {
  $dto = new DateTime();
  $dto->setISODate($year, $week);
  $ret['week_start'] = $dto->format('Y-m-d');
  $dto->modify('+6 days');
  $ret['week_end'] = $dto->format('Y-m-d');
  return $ret;
}
  $day=etStartAndEndDate($week-1,$year);
var_dump($day)
krishn Patel
  • 2,579
  • 1
  • 19
  • 30
1

You can do it easily using date and strtotime functions this way:

    echo "today is: ".date("m/d/Y", strtotime("today"))."<br/>";
echo "last Week starting date: ".date("m/d/Y", strtotime("last week monday"))."<br/>";
echo "last Week ending date: ".date("m/d/Y", strtotime("last week sunday"));
  • thank you for reply if i want to get the second week of this month how can i get – kunal Jan 31 '17 at 06:22
  • `strtotime("second monday of $month $year")`. check this [demo](https://eval.in/727348) – bansi Jan 31 '17 at 06:28
  • well, you can always use strtotime doing something like this: `date('d', strtotime('second mon of january 2017'))`; –  Jan 31 '17 at 06:31
1

I have check in example.. please try this can help you.

date_default_timezone_set('UTC');
$firstDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-6);
$lastDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-0);
echo("Last week began on: ".date("d.m.Y",$firstDayOfLastWeek));
echo("<br>");
echo("Last week ended on: ".date("d.m.Y",$lastDayOfLastWeek));
Tejas Soni
  • 551
  • 3
  • 10