How to convert timestamp into mm:dd:yy format.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamill
    New Member
    • Dec 2006
    • 71

    How to convert timestamp into mm:dd:yy format.

    I have a timestamp type field into my mysql database table. Now threw php i need to change it into mm:dd:yy formate.Threw witch function i can do it. Please help me with an examole. suppose timestamp is this 20070209162211 then how can i convert it into mm:dd:yy formate.
    ...........
    kamill
  • stephane
    New Member
    • Feb 2007
    • 35

    #2
    date( "d/m/Y", $unix_timestamp );

    but 20070209162211 it isn't unix timestamp.
    unix timestamp is a number of seconds from 1970 year

    Comment

    • stephane
      New Member
      • Feb 2007
      • 35

      #3
      20070209162211 is
      2007-02-09 16:22:11

      it's more simple to convert. split this string by symbols using substr() and its all

      Comment

      • devsusen
        New Member
        • Feb 2007
        • 136

        #4
        unix timestamp and value stored in mysql timestamp datatype are different. There is no such function which can convert mysql timestamp value to mm:dd:yy format.

        So its better to use sustr() to get year, month, date, hour, minute, seconds then you can use it. the format for mysql timestamp type is YYYYMMDDhhmmss.

        susen

        Comment

        • stephane
          New Member
          • Feb 2007
          • 35

          #5
          Originally posted by devsusen
          unix timestamp and value stored in mysql timestamp datatype are different. There is no such function which can convert mysql timestamp value to mm:dd:yy format.
          susen
          you are wrong
          there is a date() function to convert timestamp to every date format
          http://www.php.net/date

          Comment

          • sunbin
            New Member
            • Feb 2007
            • 7

            #6
            hi,

            i have used this code block many times,

            Code:
            <?
            
            $time_stamp=time(); //gives timestamp
            $date_format=date("m:d:y",$time_stamp); // formats timestamp in mm:dd:yy 
            
            print $date_format; // results here ... 02 : 11 : 07
            
            ?>
            Solved ???

            Sunbin
            Last edited by Dormilich; Oct 8 '10, 05:28 AM.

            Comment

            • devsusen
              New Member
              • Feb 2007
              • 136

              #7
              Originally posted by stephane
              you are wrong
              there is a date() function to convert timestamp to every date format
              http://www.php.net/date
              date() can accept timestamp parameter if it is only unix timestamp not the value stored in mysql table as timestamp datatype.

              Susen

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                I cannot follow if you had your reply, so I'll throw this one in to convert your YYYYMMDDHHMMSS into MM: DD:YY:

                Code:
                echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
                Ronald :cool:
                Last edited by Dormilich; Oct 8 '10, 05:28 AM.

                Comment

                • devsusen
                  New Member
                  • Feb 2007
                  • 136

                  #9
                  Originally posted by ronverdonk
                  I cannot follow if you had your reply, so I'll throw this one in to convert your YYYYMMDDHHMMSS into MM: DD:YY:

                  Code:
                  echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
                  Ronald :cool:
                  I accept this as a good solution.

                  Susen
                  Last edited by Dormilich; Oct 8 '10, 05:29 AM.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by devsusen
                    I accept this as a good solution.

                    Susen
                    I am so glad you do!

                    Ronald :cool:

                    Comment

                    • ronlg
                      New Member
                      • Aug 2008
                      • 1

                      #11
                      I just learned how to do this properly...
                      What you want to do is have it all handled in your MySQL Query...


                      In my case, which might not work for you, I have:

                      Code:
                      $select = array();
                      $select[] = "MONTH(timeStamp) AS theMonth";
                      $select[] = "DAY(timeStamp) AS theDay";
                      That's just part of my query...

                      Later, when I want to echo the results, I use:

                      Code:
                      <?=$results["theMonth"]."/".$results["theDay"];?>
                      Hopefully this helps. One of our dev. guys just showed it to me and it works like a charm!
                      Last edited by Dormilich; Oct 8 '10, 05:29 AM.

                      Comment

                      • billyb

                        #12
                        are these examples pulling the timestamp from a database row, and displaying in a new format?

                        Comment

                        Working...