-1

I am converting a string number to strtotime and then attempting to get a written date outputted. This is working to an extent but the issue is that the dates are wrong.

THE PHP

$today = date("Y-m-d");

function dateRange($start, $end) {
    date_default_timezone_set('UTC');

    $diff = strtotime($end) - strtotime($start);

    $daysBetween = floor($diff/(60*60*24));

    $formattedDates = array();
    for ($i = 0; $i <= $daysBetween; $i++) {
       // $tmpDate = date('Y-m-d', strtotime($start . " + $i days"));
        $tmpDate = date('Y-m-d', strtotime($start . " + $i days"));
      //  $formattedDates[] = date('Y-m-d', strtotime($tmpDate));
        $formattedDates[] = date('Y-m-d', strtotime($tmpDate));
    }    
    return $formattedDates;
}


$start=$date_system_installed;
$end=$today;

$formattedDates = dateRange($start, $end);

foreach ($formattedDates as $dt)
{
    $date = strtotime($dt);
    echo date('l jS F Y',$date);
}

The outputted dates

Dates table

The true dates that should be showing but in text

True Dates

Where am i going wrong for this to output the correct format but incorrect dates?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Derple
  • 865
  • 11
  • 28
  • I found [`this`](http://stackoverflow.com/q/5463903/) under Related >>> (it might help). – Funk Forty Niner Aug 06 '14 at 20:04
  • Why don't you use DateInterval and DatePeriod? See http://stackoverflow.com/questions/3207749/i-have-2-dates-in-php-how-can-i-run-a-foreach-loop-to-go-through-all-of-those-d for an example. – ax. Aug 06 '14 at 20:09
  • I just posted an example using `DateInterval` as an answer below. It was quicker than debugging the existing code :) hope that's ok – jeremyharris Aug 06 '14 at 20:11
  • I'm down voting you because you say the dates are wrong, but not how they are wrong, and your examples don't match up at all. Give us the inputs you are using to generate the bad data. – Zombiesplat Aug 06 '14 at 20:28

2 Answers2

0

Here's a date range function I wrote that generates an array of formatted dates. It uses the DateTime() functions which will give you more accurate intervals than things like 60*60/24.

/**
 * Creates an array of dates between `$start` and `$end`, intervaled by a day
 *
 * @param string $start Start date string
 * @param string $end End date string
 * @param string $format Date format
 * @param boolean $inclusive Whether or not to include the start and end dates
 * @return array Array of dates between the two
 */
    function dateRange($start = null, $end = null, $format = 'Y-m-d', $inclusive = true) {
        if (empty($start) || empty($end)) {
            return array();
        }
        $start = new DateTime($start);
        $end = new DateTime($end);
        if ($inclusive) {
            $end = $end->modify('+1 day');
        } else {
            $start = $start->modify('+1 day');
        }

        $interval = new DateInterval('P1D');
        $period = new DatePeriod($start, $interval, $end);
        $daterange = array();
        foreach ($period as $date) {
            $daterange[] = $date->format($format);
        }
        return $daterange;
    }

And a test case

function testDateRange() {
        $results = dateRange('2012-02-20', '2012-03-01');
        $expected = array(
            '2012-02-20',
            '2012-02-21',
            '2012-02-22',
            '2012-02-23',
            '2012-02-24',
            '2012-02-25',
            '2012-02-26',
            '2012-02-27',
            '2012-02-28',
            '2012-02-29',
            '2012-03-01'
        );
        $this->assertEquals($expected, $results);

        $results = dateRange('2012-02-24', '2012-03-01', 'M j');
        $expected = array(
            'Feb 24',
            'Feb 25',
            'Feb 26',
            'Feb 27',
            'Feb 28',
            'Feb 29',
            'Mar 1'
        );
        $this->assertEquals($expected, $results);

        $results = dateRange('2012-02-25', '2012-03-01', 'Y-m-d', false);
        $expected = array(
            '2012-02-26',
            '2012-02-27',
            '2012-02-28',
            '2012-02-29'
        );
        $this->assertEquals($expected, $results);
    }
jeremyharris
  • 7,884
  • 22
  • 31
0

I ran your code - it's all ok http://codepad.org/E18ccpxV

fiction
  • 566
  • 1
  • 6
  • 21