PHP Time Bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tomas

    PHP Time Bug

    I have a very interesting problem...
    Php says that the difference between these times is 1 hour.

    <?php
    $t1 = "2005-12-31 20:00";
    $t2 = "2005-12-31 20:00";
    $t1i = strtotime($t1);
    $t2i = strtotime($t2);
    $diff = abs($t1i - $t2i);
    print_r(getdate ($diff));
    ?>

    The Output:
    Array
    (
    [seconds] => 0
    [minutes] => 0
    [hours] => 1
    [mday] => 1
    [wday] => 4
    [mon] => 1
    [year] => 1970
    [yday] => 0
    [weekday] => Thursday
    [month] => January
    [0] => 0
    )

    Can you please suggest something how to calculate the difference
    between 2 datetimes?

    And another question, a bit off-topic, but how do I get the number of
    days of a month with taking leap years in consideration?

    Thanks in advence.
    Tomas

  • Marc van Lieshout

    #2
    Re: PHP Time Bug


    "Tomas" <tomas.srna@gma il.com> wrote in message
    news:1138917713 .942658.239590@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    >I have a very interesting problem...
    > Php says that the difference between these times is 1 hour.
    >
    > <?php
    > $t1 = "2005-12-31 20:00";
    > $t2 = "2005-12-31 20:00";
    > $t1i = strtotime($t1);
    > $t2i = strtotime($t2);
    > $diff = abs($t1i - $t2i);
    > print_r(getdate ($diff));
    > ?>
    >
    > The Output:
    > Array
    > (
    > [seconds] => 0
    > [minutes] => 0
    > [hours] => 1
    > [mday] => 1
    > [wday] => 4
    > [mon] => 1
    > [year] => 1970
    > [yday] => 0
    > [weekday] => Thursday
    > [month] => January
    > [0] => 0
    > )
    >
    > Can you please suggest something how to calculate the difference
    > between 2 datetimes?
    >
    > And another question, a bit off-topic, but how do I get the number of
    > days of a month with taking leap years in consideration?
    >
    > Thanks in advence.
    > Tomas
    >[/color]

    function is_leap_year($y ear) {
    return (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0);
    }

    function days_in_month($ month, $year) {

    switch ($month) {
    case 4:
    case 6:
    case 9:
    case 11:
    return 30;

    case 2:
    return is_leap_year($y ear) ? 29 : 28;

    default:
    return 31;
    }
    }


    Marc.




    Comment

    • Andy Hassall

      #3
      Re: PHP Time Bug

      On 2 Feb 2006 14:01:54 -0800, "Tomas" <tomas.srna@gma il.com> wrote:
      [color=blue]
      >I have a very interesting problem...
      >Php says that the difference between these times is 1 hour.
      >
      ><?php
      >$t1 = "2005-12-31 20:00";
      >$t2 = "2005-12-31 20:00";
      >$t1i = strtotime($t1);
      >$t2i = strtotime($t2);
      >$diff = abs($t1i - $t2i);
      >print_r(getdat e($diff));
      >?>
      >
      >The Output:
      >Array
      >(
      > [seconds] => 0
      > [minutes] => 0
      > [hours] => 1
      > [mday] => 1
      > [wday] => 4
      > [mon] => 1
      > [year] => 1970
      > [yday] => 0
      > [weekday] => Thursday
      > [month] => January
      > [0] => 0
      >)[/color]

      What you've ended up asking with the code above is "what is the year, month,
      day and time of the difference of two identical times?" - which doesn't make
      much sense.

      You end up with "what is the date represented by the value zero", which is
      defined as the "UNIX epoch". Apparently you're in a central European country
      currently on GMT+1 (Slovakia from the looks of your message headers?); the UNIX
      epoch is midnight 1st Jan 1970 GMT - the result you've got is 1am on the same
      day, as adjusted for your timezone.
      [color=blue]
      >Can you please suggest something how to calculate the difference
      >between 2 datetimes?[/color]

      Depends what you really want. You already had the difference in seconds -
      zero. You can divide this down to get hours or days, but it depends how you
      want to deal with summer time transitions.
      [color=blue]
      >And another question, a bit off-topic, but how do I get the number of
      >days of a month with taking leap years in consideration?[/color]

      The "t" option in http://uk2.php.net/date
      Or http://uk2.php.net/calendar

      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      • Pedro Graca

        #4
        Re: PHP Time Bug

        Tomas wrote:[color=blue]
        > I have a very interesting problem...
        > Php says that the difference between these times is 1 hour.[/color]

        No it doesn't
        [color=blue]
        > <?php
        > $t1 = "2005-12-31 20:00";
        > $t2 = "2005-12-31 20:00";
        > $t1i = strtotime($t1);
        > $t2i = strtotime($t2);
        > $diff = abs($t1i - $t2i);
        > print_r(getdate ($diff));
        > ?>[/color]

        What PHP says is that getdate(0) returns the following
        [color=blue]
        > The Output:
        > Array
        > (
        > [seconds] => 0
        > [minutes] => 0
        > [hours] => 1
        > [mday] => 1
        > [wday] => 4
        > [mon] => 1
        > [year] => 1970
        > [yday] => 0
        > [weekday] => Thursday
        > [month] => January
        > [0] => 0
        > )
        >
        > Can you please suggest something how to calculate the difference
        > between 2 datetimes?[/color]

        You already did that.
        The difference between $t1 and $t2 is 0 (seconds); if you want to
        convert seconds to hours divide by 3600

        echo 'Difference is ', $diff / 3600, 'hours';
        [color=blue]
        > And another question, a bit off-topic, but how do I get the number of
        > days of a month with taking leap years in consideration?[/color]

        $month = 2;
        $year = 2000;
        $number_of_days 2000 = date('t', gmmktime(12, 0, 0, $month, 15, $year));
        $year = 2004;
        $number_of_days 2004 = date('t', gmmktime(12, 0, 0, $month, 15, $year));

        --
        If you're posting through Google read <http://cfaj.freeshell. org/google>

        Comment

        Working...