month duration problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lado.Leskovec@gmail.com

    month duration problem

    Hi!

    I'm writing a script that would sort some events and calculate the sum
    of them all in the end of the month, which should be equal to the
    number of days in a month. It seems to be working well, but for some
    reason on march it calculates one hour less (30 days and 23:00:00) and
    in october one hour more (31 days 1:00:00). I've tried different years
    not just 2007. It's all the same. Anyone knows a good reason why this
    might happen as it works like a charm for other months. Please help me
    solve this, as I really dont like the solution to add another hour to
    March and subtract one from October...

    Here are some relavant parts of the code (I've translated it, so there
    might be some old variable namse in there):

    function duration($Time1 , $Time2)
    {

    $diff = $Time2 - $Time1;

    $SecondsInDay = 86400;
    $SecondsInHour = 3600;
    $SecondsInMinut e = 60;

    $days = floor($diff /$SecondsInDay );
    $hours = floor(($diff - $days *$SecondsInDay )/$SecondsInHour );
    $mins = floor(($diff - $days *$SecondsInDay - $hours *$SecondsInHour )/
    $SecondsInMinut e );
    $secs = floor($diff - $days *$SecondsInDay - $hours *$SecondsInHour -
    $mins *$SecondsInMinu te );


    $result = "$days days $hours:$mins:$s ecs";

    return $result;

    }


    $Year= $_GET['year'];
    $Month= $_GET['month'];

    $FirstTime = mktime(0,0,0,$M onth,1,$Year);
    $LastTime = mktime(0,0,0,($ Month+1),1,$Yea r);

    $FirstDate = date("d.m.Y H:i:s", $FirstTime);
    $LastDate = date("d.m.Y H:i:s", $LastTime);

    echo "$FirstDate - $LastDate";
    //these shows like it should for all months:
    //Jan: 01.01.2007 00:00:00 - 01.02.2007 00:00:00
    //Feb: 01.02.2007 00:00:00 - 01.03.2007 00:00:00
    //March: 01.03.2007 00:00:00 - 01.04.2007 00:00:00
    //April: 01.04.2007 00:00:00 - 01.05.2007 00:00:00
    //May: 01.05.2007 00:00:00 - 01.06.2007 00:00:00
    //...
    //October: 01.10.2007 00:00:00 - 01.11.2007 00:00:00

    $MonthDurationI nSeconds = $LastTime - $FirstTime;
    $MonthDuration = duration($First Time, $LastTime);

    echo "$MonthDuration InSeconds; $MonthDuration" ;
    //for two months of same length this above is different and I cant
    figure out why...
    //Jan: 2678400, 31 days 00:00:00
    //Feb: 2419200, 28 days 00:00:00
    //March: 2674800, 30 days 23:00:00
    //April: 2592000, 30 days 00:00:00
    //May: 2678400, 31 days 00:00:00
    //...
    //October: 2682000, 31 days 01:00:00
  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: month duration problem

    Lado.Leskovec@g mail.com wrote:
    [...] It seems to be working well, but for some reason on march it
    calculates one hour less (30 days and 23:00:00) and in october one hour
    more (31 days 1:00:00).
    Why not use something like this?


    $days_in_month =
    date( 't' , mktime(0,0,0,$M onth,1,$Year) );

    $MonthDurationI nSeconds = $days_in_month * 24 * 3600;




    Cheers,
    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Lado.Leskovec@gmail.com

      #3
      Re: month duration problem

      Thanks :) It worked.

      Regards,
      Lado

      Comment

      • Jerry Stuckle

        #4
        Re: month duration problem

        Lado.Leskovec@g mail.com wrote:
        Hi!
        >
        I'm writing a script that would sort some events and calculate the sum
        of them all in the end of the month, which should be equal to the
        number of days in a month. It seems to be working well, but for some
        reason on march it calculates one hour less (30 days and 23:00:00) and
        in october one hour more (31 days 1:00:00). I've tried different years
        not just 2007. It's all the same. Anyone knows a good reason why this
        might happen as it works like a charm for other months. Please help me
        solve this, as I really dont like the solution to add another hour to
        March and subtract one from October...
        >
        Here are some relavant parts of the code (I've translated it, so there
        might be some old variable namse in there):
        >
        function duration($Time1 , $Time2)
        {
        >
        $diff = $Time2 - $Time1;
        >
        $SecondsInDay = 86400;
        $SecondsInHour = 3600;
        $SecondsInMinut e = 60;
        >
        $days = floor($diff /$SecondsInDay );
        $hours = floor(($diff - $days *$SecondsInDay )/$SecondsInHour );
        $mins = floor(($diff - $days *$SecondsInDay - $hours *$SecondsInHour )/
        $SecondsInMinut e );
        $secs = floor($diff - $days *$SecondsInDay - $hours *$SecondsInHour -
        $mins *$SecondsInMinu te );
        >
        >
        $result = "$days days $hours:$mins:$s ecs";
        >
        return $result;
        >
        }
        >
        >
        $Year= $_GET['year'];
        $Month= $_GET['month'];
        >
        $FirstTime = mktime(0,0,0,$M onth,1,$Year);
        $LastTime = mktime(0,0,0,($ Month+1),1,$Yea r);
        >
        $FirstDate = date("d.m.Y H:i:s", $FirstTime);
        $LastDate = date("d.m.Y H:i:s", $LastTime);
        >
        echo "$FirstDate - $LastDate";
        //these shows like it should for all months:
        //Jan: 01.01.2007 00:00:00 - 01.02.2007 00:00:00
        //Feb: 01.02.2007 00:00:00 - 01.03.2007 00:00:00
        //March: 01.03.2007 00:00:00 - 01.04.2007 00:00:00
        //April: 01.04.2007 00:00:00 - 01.05.2007 00:00:00
        //May: 01.05.2007 00:00:00 - 01.06.2007 00:00:00
        //...
        //October: 01.10.2007 00:00:00 - 01.11.2007 00:00:00
        >
        $MonthDurationI nSeconds = $LastTime - $FirstTime;
        $MonthDuration = duration($First Time, $LastTime);
        >
        echo "$MonthDuration InSeconds; $MonthDuration" ;
        //for two months of same length this above is different and I cant
        figure out why...
        //Jan: 2678400, 31 days 00:00:00
        //Feb: 2419200, 28 days 00:00:00
        //March: 2674800, 30 days 23:00:00
        //April: 2592000, 30 days 00:00:00
        //May: 2678400, 31 days 00:00:00
        //...
        //October: 2682000, 31 days 01:00:00
        >
        But March is one hour short, and October one hour long due to daylight
        savings time.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • NC

          #5
          Re: month duration problem

          On Jul 1, 12:37 am, Lado.Lesko...@g mail.com wrote:
          >
          I'm writing a script that would sort some events and calculate the sum
          of them all in the end of the month, which should be equal to the
          number of days in a month. It seems to be working well, but for some
          reason on march it calculates one hour less (30 days and 23:00:00) and
          in october one hour more (31 days 1:00:00).
          Which is correct, because of daylight saving time. In March, clocks
          are
          moved one hour ahead, so you lose an hour. In October, clocks are set
          one
          hour back, so you gain an hour.
          Please help me solve this,
          You might recall that date('t', $timestamp) returns the number of
          days
          in the month into which the $timestamp falls. You can then use that
          number to compute the month's duration in seconds.

          Cheers,
          NC

          Comment

          • Lado.Leskovec@gmail.com

            #6
            Re: month duration problem

            I had to do some changes to my script to include longer-than-month
            periods, so I had to use the first solution again. This brought up the
            same problem again, but after thinking about what you guys said, I
            realised it's actually better this way.

            Thanks!

            Comment

            Working...