Reputation: 39
I'm searching for the best way to echo day numbers in a week. First i need to check what week there is. And then echo all dates of the week into variables.
This is what i got:
//This Week Variable dates
$this_year = date('Y');
$this_week_no = date('W');
$this_week_mon = new DateTime();
$this_week_mon->setISODate($this_year,$this_week_no);
How do i rise tue by one day?
$this_week_tue = $this_week_mon ++;
Upvotes: 0
Views: 56
Reputation: 219804
You can use DateTime::modify()
:
$this_week_mon->modify('+1 day');
or DateTime::add()
which accepts a DateInterval()
object:
$this_week_mon->add(new DateInterval('P1D'));
You can loop through all of the days of the week using DatePeriod()
:
$start = new DateTime();
$start->setISODate($this_year,$this_week_no);
$end = clone $start;
$end->modify('+1 week');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('N');
}
Upvotes: 1
Reputation: 4150
$this_week_mon->modify('+1 day');
Should increment $this_week_mon
by one day. To increase by more, just use days;
$this_week_mon->modify('+27 day');
Would increment by 27 days.
Upvotes: 0