How to format at date field output

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

    How to format at date field output

    I have a field in a database called DateRcvd. At present, it outputs in my
    report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
    format. What is the easiest way to accomplish this?
  • Jerry Stuckle

    #2
    Re: How to format at date field output

    Bob Sanderson wrote:
    I have a field in a database called DateRcvd. At present, it outputs in my
    report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
    format. What is the easiest way to accomplish this?
    Depends on the database., But most have a function to format the
    default date to however you want. Check your database documentation.

    Otherwise you could reformat it in the php code - use substr() or a
    regex to get the year, month and day, then display them like you wish.

    Personally I prefer the database function.

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

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      [PHP]<?php
      $d="2006-10-25";
      echo date("d/m/y", strtotime($d));
      ?>[/PHP]
      results in 25/10/06

      Ronald :cool:

      Comment

      • Kim André Akerø

        #4
        Re: How to format at date field output

        Bob Sanderson wrote:
        I have a field in a database called DateRcvd. At present, it outputs
        in my report in the yyyy-mm-dd format. I would like it to display in
        the dd/mm/yy format. What is the easiest way to accomplish this?
        You could reformat it using PHP:

        echo date("d/m/y",strtotime($r ow["DateRcvd"]));

        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


        --
        Kim André Akerø
        - kimandre@NOSPAM betadome.com
        (remove NOSPAM to contact me directly)

        Comment

        • Iván Sánchez Ortega

          #5
          Re: How to format at date field output

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Bob Sanderson wrote:
          I have a field in a database called DateRcvd. At present, it outputs in my
          report in the yyyy-mm-dd format. I would like it to display in the
          dd/mm/yy format. What is the easiest way to accomplish this?
          Rely on the DB's date formatting functions. Something like:

          select date_format( table.DateRcvd, '%y/%m/%d' ) as formatted_date from
          table;

          Please check your SQL DB manual for the exact syntax for those functions.
          - --
          - ----------------------------------
          Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

          Un ordenador no es un televisor ni un microondas, es una herramienta
          compleja.
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.3 (GNU/Linux)

          iD8DBQFExh5u3jc Q2mg3Pc8RAnR3AJ 9HyyXYCrNFijuaL PdffeRuYzNBagCc CcNW
          Xn0pEQgp9bdzIal xackv3RE=
          =EF4e
          -----END PGP SIGNATURE-----

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Sorry I forgot, but when you want to use MySql to format it, just do:
            Code:
            select date , date_format(date, "%d/%m/%y") as date1 from table
            then date1 contains your re-formatted date.

            Ronald :cool:

            Comment

            • Andy Jeffries

              #7
              Re: How to format at date field output

              On Tue, 25 Jul 2006 09:46:02 -0400, Jerry Stuckle wrote:
              >I have a field in a database called DateRcvd. At present, it outputs in
              >my report in the yyyy-mm-dd format. I would like it to display in the
              >dd/mm/yy format. What is the easiest way to accomplish this?
              >
              Depends on the database., But most have a function to format the default
              date to however you want. Check your database documentation.
              >
              Otherwise you could reformat it in the php code - use substr() or a regex
              to get the year, month and day, then display them like you wish.
              >
              Personally I prefer the database function.
              Personally I prefer Kim André Akerø's solution, it's easier to read and
              programmer time is (in *most* of the work I do) worth more than CPU time.
              However, Kim's method is by far the slowest. To do 10,000 iterations of
              each version:

              $ ./test.php
              strtotime/date = 0.5179 seconds
              ereg = 0.0885 seconds
              preg = 0.0435 seconds
              substr = 0.0282 seconds

              However, as I rarely do more than one or two conversions like this per
              page impression, and they are on fairly low traffic sites on powerful
              boxes (max is about 5M page impressions per month) I'll stick to most
              readable :-)

              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

              • Mladen Gogala

                #8
                Re: How to format at date field output

                Andy Jeffries wrote:
                >
                Personally I prefer Kim André Akerø's solution, it's easier to read and
                programmer time is (in *most* of the work I do) worth more than CPU time.
                However, Kim's method is by far the slowest. To do 10,000 iterations of
                each version:
                Formatting within the PHP script might be easier to read, but databases
                usually have extensive built-in calendaring capabilities. At least, that
                is the case with Oracle (my favorite), SQL Server and PostgreSQL. All
                the date rules are already built in, database "knows" what day will it
                be on 7/4/2076, will it be a leap year and what day will Halloween fall
                on this year. I find it wasteful to re-implement all those goodies in
                PHP, just for purity.
                --
                Mladen Gogala

                Comment

                • Alvaro G. Vicario

                  #9
                  Re: How to format at date field output

                  *** Bob Sanderson escribió/wrote (Tue, 25 Jul 2006 12:49:17 GMT):
                  I have a field in a database called DateRcvd. At present, it outputs in my
                  report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
                  format. What is the easiest way to accomplish this?
                  I particularly find it easier to handle dates as Unix timestamps. You don't
                  say what your DBMS is so I'll assume MySQL:

                  SELECT UNIX_TIMESTAMP( DateRcvd) FROM table

                  Once you read the value into a PHP variable, you can use almost date
                  function to format it, such as date() or strftime().

                  When you need to insert dates into MySQL:

                  INSERT INTO table (DateRcvd) VALUES (FROM_UNIXTIME( 1153851613))


                  --
                  -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
                  ++ Mi sitio sobre programación web: http://bits.demogracia.com
                  +- Mi web de humor con rayos UVA: http://www.demogracia.com
                  --

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: How to format at date field output

                    Andy Jeffries wrote:
                    On Tue, 25 Jul 2006 09:46:02 -0400, Jerry Stuckle wrote:
                    >
                    >>>I have a field in a database called DateRcvd. At present, it outputs in
                    >>>my report in the yyyy-mm-dd format. I would like it to display in the
                    >>>dd/mm/yy format. What is the easiest way to accomplish this?
                    >>
                    >>Depends on the database., But most have a function to format the default
                    >>date to however you want. Check your database documentation.
                    >>
                    >>Otherwise you could reformat it in the php code - use substr() or a regex
                    >>to get the year, month and day, then display them like you wish.
                    >>
                    >>Personally I prefer the database function.
                    >
                    >
                    Personally I prefer Kim André Akerø's solution, it's easier to read and
                    programmer time is (in *most* of the work I do) worth more than CPU time.
                    However, Kim's method is by far the slowest. To do 10,000 iterations of
                    each version:
                    >
                    $ ./test.php
                    strtotime/date = 0.5179 seconds
                    ereg = 0.0885 seconds
                    preg = 0.0435 seconds
                    substr = 0.0282 seconds
                    >
                    However, as I rarely do more than one or two conversions like this per
                    page impression, and they are on fairly low traffic sites on powerful
                    boxes (max is about 5M page impressions per month) I'll stick to most
                    readable :-)
                    >
                    Cheers,
                    >
                    >
                    Andy
                    >
                    >
                    Andy,

                    Personally, I prefer to let the database handle it. It's generally
                    faster overall - the database probably had to convert it to yyyy-mm-dd
                    format in the first place. That way it can convert directly to the
                    desire format instead of going through two conversions.

                    And even if it doesn't have to convert it to yyyy-mm-dd, the compiled
                    code in the database is generally a lot more efficient than the PHP.

                    And that's the easiest to read!

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

                    Comment

                    Working...