Reputation: 318
I have a week format from input week like so
$the_week = 2020-W01
how can I get all of the weekdays from sun to Sat of this specific week in PHP? edit
$first_date = date('Y-m-d',strtotime("W".$the_week)); //first date
$d_1 = date("Y-m-d",strtotime("+1 day", strtotime($first_date)));
$d_2 = date("Y-m-d",strtotime("+2 day", strtotime($first_date)));
$d_3 = date("Y-m-d",strtotime("+3 day", strtotime($first_date)));
$d_4 = date("Y-m-d",strtotime("+4 day", strtotime($first_date)));
$d_5 = date("Y-m-d",strtotime("+5 day", strtotime($first_date)));
$d_6 = date("Y-m-d",strtotime("+6 day", strtotime($first_date)));
i get 1999
Upvotes: 0
Views: 63
Reputation: 836
The below code is working fine
<?php
$first_date = date('Y-m-d',strtotime("W2020-W01"));
$d_1 = date("Y-m-d",strtotime("+1 day", strtotime($first_date)));
$d_2 = date("Y-m-d",strtotime("+2 day", strtotime($first_date)));
$d_3 = date("Y-m-d",strtotime("+3 day", strtotime($first_date)));
$d_4 = date("Y-m-d",strtotime("+4 day", strtotime($first_date)));
$d_5 = date("Y-m-d",strtotime("+5 day", strtotime($first_date)));
$d_6 = date("Y-m-d",strtotime("+6 day", strtotime($first_date)));
echo $d_1;
echo $d_2;
echo $d_3;
echo $d_4;
echo $d_5;
echo $d_6;
?>
What is the issue?
Upvotes: -1
Reputation: 14927
You may use the DateTime
API, get the given date then add up to 6 days using DateTime#add
and a proper DateInterval
:
$baseDay = new \DateTimeImmutable($the_week . ' -1day');
for ($dayIncrement = 0; $dayIncrement < 7; $dayIncrement++) {
$day = $baseDay->add(new \DateInterval("P{$dayIncrement}D"));
echo $day->format('d/m/Y'), PHP_EOL;
}
Demo: https://3v4l.org/sq5XK
Upvotes: 1