0

I have this code

<?php
function getDatesBetween2Dates($startTime, $endTime) {
  $day = 86400;
  $format = 'd-m-Y';
  $startTime = strtotime($startTime);
  $endTime = strtotime($endTime);
  $numDays = round(($endTime - $startTime) / $day) + 1;
  $days = array();

  for ($i = 0; $i < $numDays; $i++) {
      $days[] = date($format, ($startTime + ($i * $day)));
  }

  return $days;
}

$days = getDatesBetween2Dates(
  date('d-m-Y', strtotime("$start")),
  date('d-m-Y',strtotime("$end"))
);

foreach ($days as $key => $value) {
  // ...
}

this then goes on to search a database and display results based on a date return. I did not write this code and have no idea where i had it from.

The problem is that is is returning two 2nd november 2014??? Its messing every thing up. Any help would be awesome. I shall try and share a page link. ( the page holds personal info)

http://djwservices.co.uk/invoices/calendar.php

As a side question if anyone can get the expand/shrink to work on onclick that would be awesome.

Thanks in advance Daz

Jeff Davis
  • 4,736
  • 4
  • 38
  • 44
  • Why do people have this mistaken belief that every day is 86400 seconds long? – Mark Baker Nov 10 '14 at 14:35
  • [link](http://djwservices.co.uk/invoices/calendar.php) the page in question. – user2826526 Nov 10 '14 at 14:35
  • I realise not all days have 86400 seconds or in fact no day is 24 hours long butim still stuck! – user2826526 Nov 10 '14 at 14:37
  • `d-m-Y` is a pretty ugly format to be using as your datestrings. And why are you doing string->date->string like a drunk sailor? Going to string should be the LAST thing you do before displaying to a human. Keep things as a normal timestamp within PHP to save you multiple roundtrips through ambiguous string formats. – Marc B Nov 10 '14 at 14:40
  • As i said i didnt write this and i work under the theory.. if it aint broke, dont break it!. This worked up until 2nd November 2014. Its now broke and i cant fix it? – user2826526 Nov 10 '14 at 14:44
  • I was thinking of two very specific dates each year, when countries that have daylight savings have fewer and greater than 24 hrs in a day respectively – Mark Baker Nov 10 '14 at 14:53
  • Coincidentally, in the United states, that date with 25 hours happens to fall on the very date that appears twice in your list – Mark Baker Nov 10 '14 at 14:55
  • If you can update to php 5.3 you can use the built in functions to avoid these sort of issues. Although it would be great to update to 5.6 if you don't have compatibility problems (which, honestly, you probably will with an old code base). The newer functions make problems like this much easier to deal with. – Jeff Davis Nov 10 '14 at 15:20
  • Ok i have tried both solutions and have to go now so will try more later. Thinking about seconds in the day i tried upping the second count of my original script from 86400 to 87000 and it seems to have done the trick for now. Kind of a duct tape solution ;) – user2826526 Nov 10 '14 at 15:46
  • Of course, it may well break again on 8th Mar 2015, but hopefully you'll have implemented a sensible fix by then – Mark Baker Nov 10 '14 at 16:54

2 Answers2

0

Using PHP 5.5 Generators, you could do something like:

function period($begin, $interval, $end) {
    while ($begin <= $end) {
        yield $begin;
        $begin->add($interval);
    }
}

$interval = new \DateInterval('P1D');

$begin = new \DateTime( '2014-11-01' );
$end = new \DateTime( '2014-11-30' );

foreach (period($begin, $interval, $end) as $dt) {
    echo $dt->format( "Y-m-d" ), PHP_EOL;
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

This should be easier using the function, date_diff. From the manual:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Here is a modified version for if you don't yet have php 5.3:

function getDatesBetween2Dates($startTime, $endTime) {
    $datetime1 = new DateTime($startTime);
    $datetime2 = new DateTime($endTime);
    $interval = round(($datetime2->format('U') - $datetime1->format('U')) / (60*60*24));

    for ($i = 0; $i < $interval; $i++) {
       $dateTemp = $datetime1;
       $str = "+" . $i . " day";
       $dateTemp = $dateTemp->modify($str);
       $days[] = $dateTemp->format('d-m-Y');
    }

   return $days;
}
Jeff Davis
  • 4,736
  • 4
  • 38
  • 44
  • Thanks, tried it getting this error Fatal error: Call to undefined method DateTime::diff() in /hermes/waloraweb034/b1699/moo.myeventpicturescouk/djw/invoices/test.php on line 12 – user2826526 Nov 10 '14 at 14:57
  • Ah, you are not on php 5.3 then. Referencing this answer: http://stackoverflow.com/questions/4033224/what-can-use-for-datetimediff-for-php-5-2 – Jeff Davis Nov 10 '14 at 15:00
  • I added a php 5.2 friendly version. – Jeff Davis Nov 10 '14 at 15:04
  • Also updated it to return it in the format you wanted. – Jeff Davis Nov 10 '14 at 15:08
  • Also updated the for loop so it now includes the start date in the return values and does not include the end date (so it is more like the original) – Jeff Davis Nov 10 '14 at 15:13
  • Still no joy Fatal error: Call to undefined method DateTime::diff() in /hermes/waloraweb034/b1699/moo.myeventpicturescouk/djw/invoices/test.php on line 12 thats this line $interval = $datetime1->diff($datetime2); – user2826526 Nov 10 '14 at 15:14
  • Ok. I deleted the version that only works in php5.3+ try this version :) – Jeff Davis Nov 10 '14 at 15:16
  • Im now getting a +2days on a test page but all kinds of strange things on my calendar page – user2826526 Nov 10 '14 at 15:24
  • It sounds like it's doing something funny with the string parsing. I made an update to get around that. – Jeff Davis Nov 10 '14 at 15:30
  • Did that work? It's working for me on a test machine. – Jeff Davis Nov 10 '14 at 16:57
  • lasty try it didnt work, i will try again tomorrow. Thanks for the help so far. As a quick fix i increased my seconds in a day which has given me some time at least. – user2826526 Nov 10 '14 at 18:11