Ilians
Ilians

Reputation: 743

time difference april - march wrong value

I'm trying to get the range of a month using the following function:

static public function getMonthRange($month, $year)
{
    $date = DateTime::createFromFormat('d-m-Y','01-'.$month.'-'.$year);
    $start = $date->getTimestamp();

    return array(
        'start' => $start,
        'end' => $date->modify('first day of next month')->getTimestamp()
    );
}

Now this works fine except for march. When I checked the values by calculating the number of days in per month using: ((end - start) / 60 / 60 / 24) I found that March is 30.958333333333 days while the other months give the correct number of days.

I decided to input the dates myself in a calculation like this:

$march = ((strtotime('2012-04-01') - strtotime('2012-03-01')) / 60 / 60 / 24); //30.95833..
$april = ((strtotime('2012-05-01') - strtotime('2012-04-01')) / 60 / 60 / 24); //30
$may =   ((strtotime('2012-06-01') - strtotime('2012-05-01')) / 60 / 60 / 24); //31

It still gave me the wrong number of days for march. Is there something wrong with my approach? And more importantly; how do I fix this?

Thanks in advance.

Upvotes: 1

Views: 129

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33533

Three words: daylight savings time.

March is missing an hour.

Also, your function exists in PHP: http://php.net/manual/en/function.cal-days-in-month.php

Upvotes: 2

Related Questions