arithmetics with timestamp?

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

    arithmetics with timestamp?

    Hi everubody,

    I have a timestamp field in a mysql DB and I want to know how much time
    has past since that timestamp.

    is it possible?
    Say that I have this value in a variable -
    1) how do I get the current system timestamp?
    2) how do I subtract the first from the second?

    Thanks!

  • Janwillem Borleffs

    #2
    Re: arithmetics with timestamp?

    Super Mango wrote:[color=blue]
    > Say that I have this value in a variable -
    > 1) how do I get the current system timestamp?
    > 2) how do I subtract the first from the second?
    >[/color]

    If you are only interested in the amount of hours, you could do something
    like:

    $now = time();
    $past = time() - 86400;

    print 'The difference between $past and $now = ';
    echo ($now - $past) / 3600, ' hours';

    Of course, you can narrow this figure down to days (div 86400), hours (div
    3600), minutes (div 60) and seconds.

    These kind of calculations can also be performed by MySQL. Have a look at
    the manual @ mysql.com for more info.


    JW


    Comment

    • Super Mango

      #3
      Re: arithmetics with timestamp?

      thanks!

      But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
      how do I convert that to the "time()" format?

      Comment

      • Dave Kelly

        #4
        Re: arithmetics with timestamp?

        Super Mango wrote:[color=blue]
        > Hi everubody,
        >
        > I have a timestamp field in a mysql DB and I want to know how much time
        > has past since that timestamp.
        >
        > is it possible?
        > Say that I have this value in a variable -
        > 1) how do I get the current system timestamp?
        > 2) how do I subtract the first from the second?
        >
        > Thanks!
        >[/color]
        I can show you how to do it at a website down the wire using a bash
        script. Should be adaptable to something local and PHP.

        TIMESTAMP=$(dat e -d "`lynx -error_file=lynx _error -dump
        -connect_timeout =20 $url/timestamp.txt`" +%s)

        Comment

        • Janwillem Borleffs

          #5
          Re: arithmetics with timestamp?

          Super Mango wrote:[color=blue]
          > thanks!
          >
          > But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
          > how do I convert that to the "time()" format?
          >[/color]

          Per example, with strtotime: http://www.php.net/strtotime

          JW


          Comment

          • Gordon Burditt

            #6
            Re: arithmetics with timestamp?

            >I have a timestamp field in a mysql DB and I want to know how much time[color=blue]
            >has past since that timestamp.
            >
            >is it possible?[/color]

            Yes. Use MySQL to do it.
            [color=blue]
            >Say that I have this value in a variable -
            >1) how do I get the current system timestamp?[/color]

            now()
            [color=blue]
            >2) how do I subtract the first from the second?[/color]

            timestampdiff() . (requires MySQL 5.0 or greater).
            timediff(), but it returns the value in hours, minutes, and seconds.
            datediff(), but only the date parts are used.

            Gordon L. Burditt

            Comment

            • Pedro Graca

              #7
              Re: arithmetics with timestamp?

              Super Mango wrote:[color=blue]
              > But the thing is that the value in the DB is kept as YYYY-MM-DD hh:mm
              > how do I convert that to the "time()" format?[/color]

              select UNIX_TIMESTAMP( column) from table;

              --
              Mail to my "From:" address is readable by all at http://www.dodgeit.com/
              == ** ## !! ------------------------------------------------ !! ## ** ==
              TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
              may bypass my spam filter. If it does, I may reply from another address!

              Comment

              • Toby Inkster

                #8
                Re: arithmetics with timestamp?

                Super Mango wrote:
                [color=blue]
                > I have a timestamp field in a mysql DB and I want to know how much time
                > has past since that timestamp.[/color]

                <?php
                // Assume timestamp from database is in $ts.
                $then = strtotime($ts);
                $now = time();
                $diff = $now - $then;
                // $diff is the difference in seconds.
                ?>

                --
                Toby A Inkster BSc (Hons) ARCS
                Contact Me ~ http://tobyinkster.co.uk/contact

                Comment

                • d

                  #9
                  Re: arithmetics with timestamp?

                  "Super Mango" <liebermann@gma il.com> wrote in message
                  news:1137344888 .413098.327320@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                  > Hi everubody,
                  >
                  > I have a timestamp field in a mysql DB and I want to know how much time
                  > has past since that timestamp.
                  >
                  > is it possible?
                  > Say that I have this value in a variable -
                  > 1) how do I get the current system timestamp?
                  > 2) how do I subtract the first from the second?
                  >
                  > Thanks![/color]

                  If you're going to be doing the vast majority of your time maths outside
                  MySQL, you might want to use integers instead of timestamps - you can just
                  save the unix timestamp in the database, which saves time converting between
                  MySQL and unix times. And, of course, you can still search/sort using the
                  unix timestamp...


                  Comment

                  Working...