16

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)
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52

10 Answers10

23

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');
}
Narf
  • 14,600
  • 3
  • 37
  • 66
  • 1
    This is what I call a clever solution. +1. Perhaps it is worth mentioning that DatePeriod is available from PHP 5.3 – briosheje Apr 13 '16 at 10:49
8

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:

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
  • In case someone interested in speeds, I've made some tests with 10.000 repeats. Results without output: #1: 0.4818s #2: 0.3317s #3: 0.1785s (fastest from these) #4: 0.1885s #5: 0.2226s #6 (the accepted answer): 0.1396s (winner - you need to have PHP v5.3+ as mentioned) – err Oct 22 '17 at 17:47
4

I believe you need a for loop:

$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);


for($i = 1; $i <= 6; ++$i) {
    $next = strtotime("+$i day",$day);
    $weekOfdays[] = date("l",$next);
} 
sandino
  • 3,813
  • 1
  • 19
  • 24
3

Just use a while loop to adjust day by day. Just add another day +1:

$weekOfdays = array();
$date = time();
$next = strtotime('+6 days');
while ($date <= $next) { // loop until next six
    $weekOfdays[] = date('l', $date); // push the day name
    $date = strtotime('+1 day', $date); // add +1 on $date
}
print_r($weekOfdays);
Kevin
  • 41,694
  • 12
  • 53
  • 70
3

Using strtotime function and while-loop

$weekOfdays = array();
$current_date = time(); //get current date.
$next = strtotime('+6 days');
while ($current_date <= $next) { 
    $weekOfdays[] = date('l', $current_date); 
    $current_date = strtotime('+1 day', $current_date); // add next date
}

echo "<pre>";
print_r($weekOfdays);
echo "</pre>";

Result :

Array
(
    [0] => Friday
    [1] => Saturday
    [2] => Sunday
    [3] => Monday
    [4] => Tuesday
    [5] => Wednesday
    [6] => Thursday
)
Web Artisan
  • 1,870
  • 3
  • 23
  • 33
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
1

here is simple modification in you code which will display total days

<?php
$weekOfdays = array();
$day = date('l');
$weekOfdays[] = $day;
$day = strtotime($day);
$total_days='5';//you can increase or decrease the day to display
for($i=1;$i<=$total_days;$i++){
 $next = strtotime("+$i day",$day);
$weekOfdays[] = date("l",$next);
}
print_r($weekOfdays);

?>

And its out put

    Array ( 
[0] => Monday 
[1] => Tuesday 
[2] => Wednesday 
[3] => Thursday 
[4] => Friday 
[5] => Saturday )
0
$weekOfdays = array();
$date = date("l");// current day
for($i = 0; $i < 7; $i++) {
    $weekOfdays[] = date("l", strtotime($date . "+$i day")); 
}
var_dump($weekOfdays);
Severino Lorilla Jr.
  • 1,637
  • 4
  • 20
  • 33
0

Check the following.Here array gets 7 day name including the current day.

$weekOfdays = array();
for($i = 1,$nxtday = time(); $i<=7; $i++)
{
    $weekOfdays[] = date("l", $nxtday);
    $nxtday += 86400;
}
var_dump($weekOfdays);
Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
0
<?php
$weekOfdays = array();
$currentDay = time();
for ($i = 0; $i < 7; $i++) {
    $weekOfdays[] = date('l', $currentDay);
    $currentDay = strtotime('+1 day', $currentDay);
}
var_dump($weekOfdays);

will output (today's friday)

array (size=7)
  0 => string 'Friday' (length=6)
  1 => string 'Saturday' (length=8)
  2 => string 'Sunday' (length=6)
  3 => string 'Monday' (length=6)
  4 => string 'Tuesday' (length=7)
  5 => string 'Wednesday' (length=9)
  6 => string 'Thursday' (length=8)
Thomas
  • 11
  • 3
0
<?php
$weekOfDays = array();
$now = time();
$daysCnt = 6;
while ($daysCnt > 0) {
   $now = mktime(0,0,0,date('m',$now),date('d',$now)+1,date('Y',$now));
   $weekOfDays[] = date('l', $now);
   $daysCnt--;
}
print_r($weekOfDays);

output:

Array
(
    [0] => Saturday
    [1] => Sunday
    [2] => Monday
    [3] => Tuesday
    [4] => Wednesday
    [5] => Thursday
)

the purpose of usage mktime() instead of strtotime() is a feature:

<?php
date_default_timezone_set("Europe/Moscow");
$today = date("d-m-Y",mktime(0,0,0,4,28,2013)); // 28-04-2013, sunday
$lastWeek = strtotime("last week $today");
$thisWeek = strtotime("this week $today");
$nextWeek = strtotime("next week $today");

echo "last week - ".$lastWeek." / ".date("d-m-Y H:i\n",$lastWeek);
echo "this week - ".$thisWeek." / ".date("d-m-Y H:i\n",$thisWeek);
echo "next week - ".$nextWeek." / ".date("d-m-Y H:i\n",$nextWeek);

result:

last week - 1366574400 / 22-04-2013 00:00; expected 15-04-2013
this week - 1367179200 / 29-04-2013 00:00; expected 22-04-2013
next week - 1367784000 / 06-05-2013 00:00; expected 29-04-2013

I recommend double-check expected results when you use strtotime()

Wizard
  • 862
  • 6
  • 9