1

My php code for getting current weekend

$day = date('w');
echo $week_start = date('Y-m-d', strtotime('-'.$day.' days')).'<br>';
echo $week_end = date('Y-m-d', strtotime('+'.(6-$day).' days'));

Through this i am getting php week start date and week end date but i want all days date from start weeknd to end weeknd how can i get in php.I want days bewteen this start date and end date with the date please help me related this i am stuck here.i am new be in php and i need this for calender week

Kritika
  • 11
  • 1

2 Answers2

0

To do this manually you start at the beginning of the week, then go to the next day to find out the date (note it) and repeat for 6 days, which brings you to the end of the week. Using 0 for the first increment, doesnt move you forward, just uses the currently set date.

// find the TIMESTAMP for the start of the week
$day = date('w');
$week_start = strtotime('-'.$day.' days');
// loop through 7 days
$dates = [];
for ($loop=0;$loop<7;$loop++) {
    $dates[] = date('Y-m-d', strtotime('+'.$loop.' days', $week_start));
}
var_dump($dates);

After removing the extra bracket, it produces:

array (size=7)
  0 => string '2018-11-04' (length=10)
  1 => string '2018-11-05' (length=10)
  2 => string '2018-11-06' (length=10)
  3 => string '2018-11-07' (length=10)
  4 => string '2018-11-08' (length=10)
  5 => string '2018-11-09' (length=10)
  6 => string '2018-11-10' (length=10)
0

// Loop from the start date to end date and output all dates in between

for ($i=$week_start; $i<=$week_end ; $i+=86400){
    echo date("Y-m-d", $i).'<br />'; }