1

I have 2 dates

$st_date = '2012-07-20';
$ed_date = '2012-07-27';

I want to display all dates from $st_date to $ed_date As:

2012-07-20
2012-07-21
2012-07-22
2012-07-23
2012-07-24
2012-07-25
2012-07-26
2012-07-27

I thought first count difference, run foreach < count, and add $i day to $st_date.
But i don't want loop, it increases code. Any of direct date() which return an array of all-dates.

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
Frank
  • 2,285
  • 7
  • 43
  • 68

6 Answers6

4

try this code :

<?php
$st_date = '2012-07-20';
$ed_date = '2012-07-27';

$dateMonthYearArr = array();
$st_dateTS = strtotime($st_date);
$ed_dateTS = strtotime($ed_date);

for ($currentDateTS = $st_dateTS; $currentDateTS <= $ed_dateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}

echo  “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>
Fredy
  • 2,840
  • 6
  • 29
  • 40
3

Whilst I don't understand your aversion to loops, I do understand hiding code away as much as possible.

To that end, I would extend PHP's DateTime object to give the functionality you are after. Something like this:-

class MyDateTime extends DateTime
{
    /**
    * Creates an array of date strings of all days between
    * the current date object and $endDate
    * 
    * @param DateTime $endDate
    * @return array of date strings
    */
    public function rangeOfDates(DateTime $endDate)
    {
        $result = array();
        $interval = new DateInterval('P1D');
        //Add a day as iterating over the DatePeriod
        //misses the last day for some strange reason
        //See here http://www.php.net/manual/en/class.dateperiod.php#102629
        $endDate->add($interval); 
        $period = new DatePeriod($this, $interval, $endDate);
        foreach($period as $day){
            $result[] = $day->format('Y-m-j');
        }
        return $result;
    }
}

Then when you want to use it, you can do this:-

$st_date = new MyDateTime("2012-07-20");
$en_date = new DateTime("2012-07-27");
$dates = $st_date->rangeOfDates($en_date);
var_dump($dates);

Will give the following output:-

array
  0 => string '2012-07-20' (length=10)
  1 => string '2012-07-21' (length=10)
  2 => string '2012-07-22' (length=10)
  3 => string '2012-07-23' (length=10)
  4 => string '2012-07-24' (length=10)
  5 => string '2012-07-25' (length=10)
  6 => string '2012-07-26' (length=10)
  7 => string '2012-07-27' (length=10)

Although, unfortunately, you will probably need a loop to iterate over that array :)

Obviously, this solution uses loops to achieve its goal, but they are encapsulated in a nice re-usable piece of code.

See the PHP manual on DateTime, DateInterval and DatePeriod for more information. There are lots of tips in the comments to those pages.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
2
$start = strtotime('2009-02-01'); 
$end = strtotime('2009-03-10'); 
$range = array();

$date = strtotime("-1 day", $start);  
while($date < $end)  { 
   $date = strtotime("+1 day", $date);
   $range[] = date('Y-m-d', $date);
} 
HamZa
  • 14,671
  • 11
  • 54
  • 75
Sergii Stotskyi
  • 5,134
  • 1
  • 22
  • 21
2

Without loop using range() & array_map() :

EDIT: a little mistake, you have to jump 86400, because 1 day = 86400 seconds, so the code should be fine now :)

    $st_date = '2012-07-20';
    $ed_date = '2012-07-27';
    $dates = range(strtotime($st_date), strtotime($ed_date),86400);
    $range_of_dates = array_map("toDate", $dates);
    print_r($range_of_dates);
    function toDate($x){return date('Y-m-d', $x);}

?>
HamZa
  • 14,671
  • 11
  • 54
  • 75
  • ok, so range and array_map = 2 loops =) But looks more elegantly – Sergii Stotskyi Jul 19 '12 at 16:59
  • @Serjio lol, i don't see any for/while/do statement, but you're right, it's indeed somehow a loop, but with this kind of problem there is no other way :) – HamZa Jul 19 '12 at 18:34
1
$st_date = '2012-07-20';
$ed_date = '2012-07-27';

for($i=$st_date;$i<=$ed_date;$i++)echo $i."<br />";
000
  • 3,976
  • 4
  • 25
  • 39
0

Little late to the party, but was tackling this issue earlier and did it with the following, which is cleaner than other examples here, so adding it as an option for people;

function dateRange($from, $to)
{
    return array_map(function($arg) {
         return date('Y-m-d', $arg);
    }, range(strtotime($from), strtotime($to), 86400));
}

Usage:

$dates = dateRange('2017-01-01', '2017-01-08');

Returns:

Array
(
    [0] => 2017-01-01
    [1] => 2017-01-02
    [2] => 2017-01-03
    [3] => 2017-01-04
    [4] => 2017-01-05
    [5] => 2017-01-06
    [6] => 2017-01-07
    [7] => 2017-01-08
)
MrMarlow
  • 856
  • 4
  • 17