简体   繁体   中英

getting next 6 days from current day?

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:

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);
} 

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);

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
)

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 )
$weekOfdays = array();
$date = date("l");// current day
for($i = 0; $i < 7; $i++) {
    $weekOfdays[] = date("l", strtotime($date . "+$i day")); 
}
var_dump($weekOfdays);

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);
<?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)
<?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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM