PHP Get Start and End Date of a Week by Weeknumber

PHP get start and end date of a week by weeknumber

Many years ago, I found this function:

function getStartAndEndDate($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;
}

$week_array = getStartAndEndDate(52,2013);
print_r($week_array);

Find the week start and end dates of all weeks between two week numbers

try this

$signupdate='2014-05-17';
$signupweek=date("W",strtotime($signupdate));
$year=date("Y",strtotime($signupdate));
$currentweek = date("W");

for($i=$signupweek;$i<=$currentweek;$i++) {
$result=getWeek($i,$year);
echo "Week:".$i." Start date:".$result['start']." End date:".$result['end']."<br>";
}

function getWeek($week, $year) {
$dto = new DateTime();
$result['start'] = $dto->setISODate($year, $week, 0)->format('Y-m-d');
$result['end'] = $dto->setISODate($year, $week, 6)->format('Y-m-d');
return $result;
}

Output

Week:20 Start date:2014-05-11 End date:2014-05-17
Week:21 Start date:2014-05-18 End date:2014-05-24
Week:22 Start date:2014-05-25 End date:2014-05-31
Week:23 Start date:2014-06-01 End date:2014-06-07
Week:24 Start date:2014-06-08 End date:2014-06-14
Week:25 Start date:2014-06-15 End date:2014-06-21
Week:26 Start date:2014-06-22 End date:2014-06-28
Week:27 Start date:2014-06-29 End date:2014-07-05
Week:28 Start date:2014-07-06 End date:2014-07-12
Week:29 Start date:2014-07-13 End date:2014-07-19
Week:30 Start date:2014-07-20 End date:2014-07-26
Week:31 Start date:2014-07-27 End date:2014-08-02
Week:32 Start date:2014-08-03 End date:2014-08-09
Week:33 Start date:2014-08-10 End date:2014-08-16

Get current week start/end date with DST

I am aware that there is more than one way to skin a cat, but in this case I was interested in how to fix my current code and more importantly to find out what's wrong with it.

Thank you all for your suggestions, especially to @misorude for pointing out the obvious flaw in my initial code, whereas "not every day has 86400 seconds", which is especially true during DST.

So here's the updated working code using relative "days" instead of fixed seconds amount:

$monday = strtotime('next Monday -1 week');
$monday = date('w', $monday)==date('w') ? strtotime(date("Y-m-d",$monday)." +7 days") : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
echo "This week start/end date:<br>";
echo $this_week_sd = date("Y-m-d",$monday)."<br>";
echo $this_week_ed = date("Y-m-d",$sunday)."<br>";

//output:
This week start/end date:
2018-10-29
2018-11-04

Once again, thank you all for your inputs. Much appreciated!

Get Start and End Days for a Given Week in PHP

I would take advantange of PHP's strtotime awesomeness:

function x_week_range(&$start_date, &$end_date, $date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
$start_date = date('Y-m-d', $start);
$end_date = date('Y-m-d', strtotime('next saturday', $start));
}

Tested on the data you provided and it works. I don't particularly like the whole reference thing you have going on, though. If this was my function, I'd have it be like this:

function x_week_range($date) {
$ts = strtotime($date);
$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)));
}

And call it like this:

list($start_date, $end_date) = x_week_range('2009-05-10');

I'm not a big fan of doing math for things like this. Dates are tricky and I prefer to have PHP figure it out.

Get week numbers with start and end dates between two dates in php

Another alternative if to use DateTime class with the ->modify method. It helps to you adjusts the date via relative dates.

Here's the idea: (the markup is up to you, or put it inside an array, whichever you prefer)

$i = 1; // week number starts at 1
$date1 = new DateTime('2018-03-21');
$date2 = new DateTime('2018-05-14');
while (true) {
echo "Week {$i}\n";
echo $date1->format('Y-m-d'), ' - ';
$date1->modify('next saturday');
if ($date1 >= $date2) {
echo $date2->format('Y-m-d');
break; // stop the loop, reached the end date, echo the last date, or whatever you want
}
echo $date1->format('Y-m-d') , "\n";
$date1->modify('next monday'); // start again on monday
$i++; // increment for next week
}

Here's a sample fiddle

Sidenote: If you want to know more or for in-depth info about what you can use on relative dates, here's the documentation entry:

http://php.net/manual/en/datetime.formats.relative.php

get start and end date of month by week number of the month in php

As for your function above in the question I have no idea, but assuming the week starts on Monday and ends on Sunday something like this might work

function getFirstandLastDate($year, $month, $week) {

$thisWeek = 1;

for($i = 1; $i < $week; $i++) {
$thisWeek = $thisWeek + 7;
}

$currentDay = date('Y-m-d',mktime(0,0,0,$month,$thisWeek,$year));

$monday = strtotime('monday this week', strtotime($currentDay));
$sunday = strtotime('sunday this week', strtotime($currentDay));

$weekStart = date('d M y', $monday);
$weekEnd = date('d M y', $sunday);

return $weekStart . ' - ' . $weekEnd;
}

echo getFirstandLastDate( 2016, 1, 1 );


Related Topics



Leave a reply



Submit