Jump to content

Recommended Posts

I am currently working on a date range script and wondering if anybody think of an easier way to do this, doing up to next 8 weeks?  This one starts on a Sunday. I also need to do one starting on a Monday (will figure that one later)

$thisweek = date("Y-m-d");
$week = date('w', strtotime($thisweek));
$date = new DateTime($thisweek);
$firstWeek = $date->modify("-".$week." day")->format("M d");
$endWeek = $date->modify("+6 day")->format("M d");
echo $firstWeek." - ";
echo $endWeek;

$nextweek = date(("Y-m-d"), strtotime("+7 Days"));
$week1 = date('w', strtotime($nextweek));
$date1 = new DateTime($nextweek);
$firstWeek1 = $date1->modify("-".$week1." day")->format("M d");
$endWeek1 = $date1->modify("+6 day")->format("M d");
echo $firstWeek1." - ";
echo $endWeek1;

 

Link to comment
https://forums.phpfreaks.com/topic/309260-date-range/
Share on other sites

Use a DatePeriod

$dt1 = new DateTime('next sunday');
$diwk = new DateInterval('P7D');
$di6 = new DateInterval('P6D');

$num_weeks = 8;
$period = new DatePeriod($dt1, $diwk, $num_weeks-1);
foreach ($period as $d) {
    echo $d->format('M d') . ' – ' ;
    $end = $d->add($di6)->format('M d');
    echo "$end<br>";
} 

 

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/309260-date-range/#findComment-1569853
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.