Hi.
I've used this function to calculate the number of days between to dates for a while, but when I tried to use it on some dates it suddenly gave me a result I can't quite understand. I try to calculate the number of days between 2007-10-31 and 2007-10-28 with this function:

function get_numdaysnew($from, $to){
$fromcmp	=	explode("-", $from);
$tocmp		=	explode("-", $to);
$fromcmpmk	=	mktime(0,0,0,$fromcmp[1],$fromcmp[2],$fromcmp[0]);
$tocmpmk	=	mktime(0,0,0,$tocmp[1],$tocmp[2],$tocmp[0]);
$diff		=	(($tocmpmk - $fromcmpmk) / 86400);
return $diff;
} //end function get_numdaysnew

$date_1	=	'2007-10-28';
$date_2	=	'2007-10-31';
$numday	=	get_numdaysnew($date_1, $date_2);

The function returns the result 3.04166666667. I can't understand this since I've set the hour/minute/second to 0 in my use of mktime-function.

Anybody to explain or to help me with a better function please?

Help would be much appreciated.

    hi

    0.041666666666 * 86400 is ~= 3600 seconds

    so somewhere is 1 hour is added
    strange???

    I cant understand this, but sure someone can ....

    edit.
    Well, here it is.
    There is an extra parameter, and you can try set this to 0, maybe.

    is_dst

    This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results. Some times are invalid if DST is enabled on the system PHP is running on or is_dst is set to 1. If DST is enabled in e.g. 2:00, all times between 2:00 and 3:00 are invalid and mktime() returns an undefined (usually negative) value.[/QUOTE]

    By the way.
    Here is a guy that has discovered exactly same thing as you:
    http://php.net/manual/en/function.mktime.php#77130

    Expected 86400 sec, but got 90000 ( +3600 )

    🙂

      Thanks a lot. That did it for me. If anyone can think of any reason NOT to set is_dst to '0' in the mktime-function I would appreciate if you let me know. For now, that seems to have solved the problem for me 🙂

        here is the reason:
        http://php.net/manual/en/function.mktime.php#77827

        mktime
        jakethecake
        14-Sep-2007 05:10
        @ Peter W (16-Aug-2007 12:08)

        The reason the difference between 28th & 29th Oct is 3600 seconds more than 27th & 28th
        is because the clocks go back on the 28th October 2007, hence the extra hour...

        I was just caught out with the same thing 🙂

          Write a Reply...