I try to store next 6 days from current day ,but I'm little stuck here how to store all next 6days in $weekOfdays
.Is there any simple function to do that?
<?php
$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);
$next = strtotime("+6 day",$day);
$weekOfdays[] = date("l",$next);
var_dump($weekOfdays);
// output
// array (size=2)
// 0 => string 'Monday' (length=6)
// 1 => string 'Sunday' (length=6)
?>
I want to see array is like this
array (size=7)
0 => string 'Monday' (length=6)
1 => string 'Tuesday' (length=7)
2 => string 'Wednesday' (length=9)
3 => string 'Thursday' (length=8)
4 => string 'Friday' (length=6)
5 => string 'Saturday' (length=8)
6 => string 'Sunday' (length=6)
Here's one that doesn't directly rely on doing the math on your own:
$days = []; $period = new DatePeriod( new DateTime(), // Start date of the period new DateInterval('P1D'), // Define the intervals as Periods of 1 Day 6 // Apply the interval 6 times on top of the starting date ); foreach ($period as $day) { $days[] = $day->format('l'); }
There are many way some of sample are mentioned as below:
1) using strtotime
function and temp $date
variable in loop
$date = date('Y-m-d'); //today date
$weekOfdays = array();
for($i =1; $i <= 7; $i++){
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$weekOfdays[] = date('l : Y-m-d', strtotime($date));
}
print_r($weekOfdays);
2) using strtotime
function and +$i days
from current date
$date = date('Y-m-d'); //today date
$weekOfdays = array();
for($i =1; $i <= 7; $i++){
$weekOfdays[] = date('l : Y-m-d', strtotime("+$i day", strtotime($date)));
}
print_r($weekOfdays);
3) using DateTime
class and modify
method
$date = date('Y-m-d'); //today date
$weekOfdays = array();
$date = new DateTime($date);
for($i=1; $i <= 7; $i++){
$date->modify('+1 day');
$weekOfdays[] = $date->format('l : Y-m-d');
}
print_r($weekOfdays);
4) using DateTime
class and DateInterval
class
$date = date('Y-m-d'); //today date
$weekOfdays = array();
$date = new DateTime($date);
for($i=1; $i <= 7; $i++){
$date->add(new DateInterval('P1D'));
$weekOfdays[] = $date->format('l : Y-m-d');
}
print_r($weekOfdays);
5) using DatePeriod
, DateInterval
and DateTime
class
$date = date('Y-m-d', strtotime('+1 day')); //tomorrow date
$weekOfdays = array();
$begin = new DateTime($date);
$end = new DateTime($date);
$end = $end->add(new DateInterval('P7D'));
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $dt){
$weekOfdays[] = $dt->format('l : Y-m-d');
}
print_r($weekOfdays);
Today is Tuesday, 12 April so output of all code will be:
Array
(
[0] => Wednesday : 2016-04-13
[1] => Thursday : 2016-04-14
[2] => Friday : 2016-04-15
[3] => Saturday : 2016-04-16
[4] => Sunday : 2016-04-17
[5] => Monday : 2016-04-18
[6] => Tuesday : 2016-04-19
)
For more detail have a look at:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With