subtract 1 day

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

    subtract 1 day

    I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix
    time stamp and the result is 23 hours earlier, not 24.
    Start with a unix time stamp:

    $unixtime=11440 18006;

    convert it to a date:
    $date=getdate($ unixtime);
    date Array
    (
    [seconds] => 46
    [minutes] => 46
    [hours] => 17
    [mday] => 2
    [wday] => 0
    [mon] => 4
    [year] => 2006
    [yday] => 91
    [weekday] => Sunday
    [month] => April
    [0] => 1144018006
    )
    date


    subtract 1 day:
    $gobackseconds= 60*60*24 ;
    $unixtime2=$uni xtime-$gobackseconds;

    and see what time it is:
    $date2=getdate( $unixtime2);


    date2 Array
    (
    [seconds] => 46
    [minutes] => 46
    [hours] => 16
    [mday] => 1
    [wday] => 6
    [mon] => 4
    [year] => 2006
    [yday] => 90
    [weekday] => Saturday
    [month] => April
    [0] => 1143931606
    )
    date2

    Notice that in the first date, there are 17 hours, and in the second
    date there are 16 hours. Arrrrrrg. What is the problem ?
  • Andy Jeffries

    #2
    Re: subtract 1 day

    On Mon, 03 Apr 2006 08:06:55 +0000, meltedown wrote:[color=blue]
    > I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix
    > time stamp and the result is 23 hours earlier, not 24. Start with a unix
    > time stamp:[/color]

    Try doing this instead:

    $unixtime2 = strtotime("-1 day", $unixtime);

    I couldn't see what you were doing wrong in your code from a quick glance,
    but the above way is easier to read anyway.

    Cheers,


    Andy

    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • meltedown

      #3
      Re: subtract 1 day

      Andy Jeffries wrote:[color=blue]
      > On Mon, 03 Apr 2006 08:06:55 +0000, meltedown wrote:[color=green]
      >> I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix
      >> time stamp and the result is 23 hours earlier, not 24. Start with a unix
      >> time stamp:[/color]
      >
      > Try doing this instead:
      >
      > $unixtime2 = strtotime("-1 day", $unixtime);
      >
      > I couldn't see what you were doing wrong in your code from a quick glance,
      > but the above way is easier to read anyway.
      >
      > Cheers,
      >
      >
      > Andy
      >[/color]
      Thanks that really helped a lot, five minutes to fix what I've been
      banging my head against all night. Funny thing is that code worked fine
      for a year and it still works for most dates but it hit that one date
      and didn't work right.

      Comment

      • Andy Jeffries

        #4
        Re: subtract 1 day

        On Mon, 03 Apr 2006 08:59:19 +0000, meltedown wrote:[color=blue][color=green]
        >> Try doing this instead:
        >>
        >> $unixtime2 = strtotime("-1 day", $unixtime);
        >>
        >> I couldn't see what you were doing wrong in your code from a quick
        >> glance, but the above way is easier to read anyway.
        >>[/color]
        > Thanks that really helped a lot, five minutes to fix what I've been
        > banging my head against all night.[/color]

        It's always the way with coding.... Glad I could help.
        [color=blue]
        > Funny thing is that code worked fine
        > for a year and it still works for most dates but it hit that one date and
        > didn't work right.[/color]

        It's very odd. I've had another quick glance at your code and still can't
        see anything obviously wrong. Never mind, you've got a solution now.

        Cheers,


        Andy

        --
        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
        http://www.gphpedit.org | PHP editor for Gnome 2
        http://www.andyjeffries.co.uk | Personal site and photos

        Comment

        • Kimmo Laine

          #5
          Re: subtract 1 day

          "meltedown" <groups2@reenie .org> wrote in message
          news:Gt5Yf.2028 83$G_2.26129@fe 08.news.easynew s.com...[color=blue]
          > Andy Jeffries wrote:[color=green]
          >> On Mon, 03 Apr 2006 08:06:55 +0000, meltedown wrote:[color=darkred]
          >>> I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix
          >>> time stamp and the result is 23 hours earlier, not 24. Start with a unix
          >>> time stamp:[/color]
          >>
          >> Try doing this instead:
          >>
          >> $unixtime2 = strtotime("-1 day", $unixtime);
          >>
          >> I couldn't see what you were doing wrong in your code from a quick
          >> glance,
          >> but the above way is easier to read anyway.
          >>
          >> Cheers,
          >>
          >>
          >> Andy
          >>[/color]
          > Thanks that really helped a lot, five minutes to fix what I've been
          > banging my head against all night. Funny thing is that code worked fine
          > for a year and it still works for most dates but it hit that one date and
          > didn't work right.[/color]

          I had the same effect when clocks were turned +1 hour to daylight saving
          time on March 26th here in Finland. I had two dates of which one was in
          normal time and another that was in daylight saving time and I kept
          wondering why the difference between them is was 5 days and 23 hours or
          something like that, until the whole daylight saving thing finally dawned to
          me. I fixed it by using GMT times which are not affected by DST.

          --
          "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
          spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


          Comment

          Working...