php days between two dates list

2019-05-31 14:25发布

问题:

Do you know what the problem is by looking at the code?

I would be happy if you helped me:

list($from_day,$from_month,$from_year)    = explode(".","27.09.2012");
list($until_day,$until_month,$until_year) = explode(".","31.10.2012");

$iDateFrom = mktime(0,0,0,$from_month,$from_day,$from_year);
$iDateTo   = mktime(0,0,0,$until_month,$until_day,$until_year);

while ($iDateFrom <= $iDateTo) {
    print date('d.m.Y',$iDateFrom)."<br><br>";
    $iDateFrom += 86400; 
}

Date of writing the same problem 2 times

October (31) for writing 2 times in history draws the ends October 30th: (

27.09.2012

28.09.2012

...

26.10.2012

27.10.2012

[[28.10.2012]]

[[28.10.2012]]

29.10.2012

30.10.2012

回答1:

  1. Your problem is because you have set time to 00:00:00, set it to 12:00:00. That is because the Daylight saving time.
  2. Stop using date() function, use Date and Time classes.

Solution (PHP >= 5.4):

$p = new DatePeriod(
    new DateTime('2012-09-27'),
    new DateInterval('P1D'),
    (new DateTime('2012-10-31'))->modify('+1 day')
);
foreach ($p as $d) {
    echo $d->format('d.m.Y') . "\n";
}

Solution (PHP < 5.4)

$end = new DateTime('2012-10-31');
$end->modify('+1 day');
$p = new DatePeriod(
    new DateTime('2012-09-27'),
    new DateInterval('P1D'),
    $end
);
foreach ($p as $d) {
    echo $d->format('d.m.Y') . "\n";
}


回答2:

You have daylight savings time issues. Adding seconds from one timestamp to another is prone to problems around these sorts of edge conditions (leap days can be problematic is well), You should get in the habit of using PHP's DateTime and DateInterval objects. It makes working with dates a snap.

$start_date = new DateTime('2012-09-27');
$end_date = new DateTime('2012-10-31');
$current_date = clone $start_date;
$date_interval = new DateInterval('P1D');

while ($current_date < $end_date) {
    // your logic here

    $current_date->add($date_interval);
}


回答3:

My idea for solving this would be something like this;

$firstDate = "27.09.2012";
$secondDate = "31.10.2012";

$daysDifference = (strtotime($secondDate) - strtotime($firstDate)) / (60 * 60 * 24);
$daysDifference = round($daysDifference);

for ($i = 0; $i <= $daysDifference; $i++)
{
    echo date("d.m.Y", strtotime('+'.$i.' day', strtotime($firstDate))) . "<BR>";
}

This should solve your problem and be much easier to read (imho). I've just tested the code, and it outputs all dates and no doubles. It also saves you from all the daylight savings inconsistencies.



回答4:

I don't know where you're from, but it's likely you're hitting daylight saving changeover in your timezone (it's Nov 4th where I live - exactly one week after Oct 28th). You can not rely on a day being exactly 86400 seconds long.

If you loop incrementing with mktime, you should be fine:

list($from_day,$from_month,$from_year)    = explode(".","27.09.2012");
list($until_day,$until_month,$until_year) = explode(".","31.10.2012");

$iDateFrom = mktime(0,0,0,$from_month,$from_day,$from_year);
$iDateTo   = mktime(0,0,0,$until_month,$until_day,$until_year);

while ($iDateFrom <= $iDateTo)
{
    print date('d.m.Y',$iDateFrom)."<br><br>";
    $from_day = $from_day + 1;
    $iDateFrom = mktime(0,0,0,$from_month,$from_day,$from_year);
}

Even though $from_day will likely be going well over 31, mktime will make the math conversion for you. (ie 32 days in a 31 day month = day 1 of the next month)

EDIT: sorry, I had the incrementation in the wrong place.



标签: php list days