Dreamweaver help: using PHP/MySQL formatting date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rodrigo21
    New Member
    • Mar 2008
    • 20

    Dreamweaver help: using PHP/MySQL formatting date

    hello,

    On a dynamic html page I have a table with a date column

    [code=html]<td><?php echo $row_resultado_ viajes['fecha']; ?>&nbsp; </td>[/code]

    I want the date to be displayed in other format so I am using the date function

    [code=html]<td><?php echo date("j-m-Y",$row_resulta do_viajes['fecha']); ?>&nbsp; </td>[/code]

    but I am getting a strange date echoed (31-12-1969). The day seems to be correct but the month and year are far from being right.

    thanks,
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What exact format (type and content) is the variable $row_resultado_ viajes['fecha'].

    As you know, date() works with a format description (1st parm) and the time in integer (2nd parm).

    Ronald

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      When that field is a database date field in MySQL DATE format (YYYY-MM-DD), then convert the database date field to integer and then reformat that to the desired output format, like[php]
      echo date("j-m-Y", strtotime($row_ resultado_viaje s['fecha'));[/php]Ronald

      Comment

      • aktar
        New Member
        • Jul 2006
        • 105

        #4
        You could also format the date in your mysql query

        see the following page:

        Comment

        • rodrigo21
          New Member
          • Mar 2008
          • 20

          #5
          Originally posted by ronverdonk
          When that field is a database date field in MySQL DATE format (YYYY-MM-DD), then convert the database date field to integer and then reformat that to the desired output format, like[php]
          echo date("j-m-Y", strtotime($row_ resultado_viaje s['fecha'));[/php]Ronald
          Thanks ronverdonk! it works fine now :)

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by aktar
            You could also format the date in your mysql query

            see the following page:

            http://dev.mysql.com/doc/refman/5.0/...on_date-format
            rodrigo never said that this field comes from MySQL. Could be Oracle, DB/2 or even Access.

            And, by the way, this is the PHP forum, not the MySQL forum.

            Ronald

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by rodrigo21
              Thanks ronverdonk! it works fine now :)
              You are welcome. See you next time.

              Ronald

              Comment

              • rodrigo21
                New Member
                • Mar 2008
                • 20

                #8
                Originally posted by aktar
                You could also format the date in your mysql query

                see the following page:

                http://dev.mysql.com/doc/refman/5.0/...on_date-format

                Yes, indeed I tried a lot of time to do it but I am a newbie in this and could not code it correctly in my dreamweaver code edit I now the code should be something like this (for example)

                SELECT DATE_FORMAT(fec ha, '%W %M %Y');

                but don't know how to open a mysql code in dreamweaver code editor for this pourpuse.

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Originally posted by rodrigo21
                  Yes, indeed I tried a lot of time to do it but I am a newbie in this and could not code it correctly in my dreamweaver code edit I now the code should be something like this (for example)

                  SELECT DATE_FORMAT(fec ha, '%W %M %Y');

                  but don't know how to open a mysql code in dreamweaver code editor for this pourpuse.
                  The correct MySQL format for this query would be in your case:[code=mysql]select date_format(fec ha, "%d-%m-%Y") as fecha;[/code]Ronald

                  Comment

                  • rodrigo21
                    New Member
                    • Mar 2008
                    • 20

                    #10
                    Originally posted by ronverdonk
                    The correct MySQL format for this query would be in your case:[code=mysql]select date_format(fec ha, "%d-%m-%Y") as fecha;[/code]Ronald
                    ok, but hoe should I insert that mysql statement into my page code? I tried with:

                    mysql>.........
                    mysql = ......
                    sql =.......

                    but now success.

                    thanks

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Since I don't use Dreamweaver (it's much nicer to write your own code) I have no idea how MySQL fits in it. However, I can show you the PHP code that would be used to access the MySQL database.

                      How you do that in DW I have no idea, but I will change the thread title a bit and let's hope that some DW guru sees it and can help you further.

                      The PHP code would be someting like[php]// Make a MySQL Connection
                      $conn = mysql_connect(" localhost", "userid", "password")
                      or die("Could not connect to the database server: ".mysql_error() );

                      // select the database
                      mysql_select_db ("db_name", $conn)
                      or die("Could not select the database: " . mysql_error());

                      // construct your SQL statement
                      $sql="SELECT DATE_FORMAT(fec ha, "%d-%m-%Y") as fecha FROM table_name WHERE your_condition" ;

                      // execute the sql statement
                      $result=mysql_q uery($sql)
                      or die("SELECT error: ".mysql_error() );

                      // no rows selected: exit with message
                      if (mysql_num_rows < 1)
                      die ("No rows selected");

                      // fetch the selected row
                      $row_resultado_ viajes = mysql_fetch_ass oc($result);

                      // Here follows the code you showed at the start of the thread
                      ?>
                      <td><?php echo $row_resultado_ viajes['fecha']; ?>&nbsp; </td>[/php]Ronald

                      Comment

                      • aktar
                        New Member
                        • Jul 2006
                        • 105

                        #12
                        Originally posted by ronverdonk
                        rodrigo never said that this field comes from MySQL. Could be Oracle, DB/2 or even Access.
                        Ronald
                        Read the thread title old man

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          Originally posted by aktar
                          Read the thread title old man
                          Not sure the 'old man' was necessary.
                          And i'm pretty sure the thread title has been changed from what it was originally.

                          Comment

                          Working...