Current date as the default value of a date field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharmilah
    New Member
    • Jun 2007
    • 20

    Current date as the default value of a date field

    Hi all

    It's me again. I now want to specify the current date in the format '26/06/2007' ie date/month/year as the default value in my mysql database. Which implies that by default the value of my date field in the database should be the current date. How can I do this in the Mysql database itself, so that each time my application is called the current date appears by default.

    Thanks in advance for your replies
  • sumaabey
    New Member
    • Jun 2007
    • 29

    #2
    echo $d=date("d/m/Y");

    Comment

    • sharmilah
      New Member
      • Jun 2007
      • 20

      #3
      Is it possible to specify the date format (e.g 26/06/2007) in the mysql database itself instead of converting it via php.

      Another question : I want also to specify the format of a numeric field (999,999,999,99 9) in the database itself so that all numbers input get this format.

      Thanks again

      Comment

      • sumaabey
        New Member
        • Jun 2007
        • 29

        #4
        select date_format(tes t_date,'%d/%m/%Y') as test_date from test_table

        this query will display the format that you need

        do you want this?

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, sharmilah.

          Originally posted by sharmilah
          Is it possible to specify the date format (e.g 26/06/2007) in the mysql database itself instead of converting it via php.
          Yes. But it hasn't been implemented yet.

          [code=mysql]SHOW VARIABLES LIKE 'date%';[/code]

          Originally posted by sharmilah
          Another question : I want also to specify the format of a numeric field (999,999,999,99 9) in the database itself so that all numbers input get this format.
          Can't specify a format for numeric types, but you can FORMAT() them.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by sharmilah
            Is it possible to specify the date format (e.g 26/06/2007) in the mysql database itself instead of converting it via php.

            Another question : I want also to specify the format of a numeric field (999,999,999,99 9) in the database itself so that all numbers input get this format.

            Thanks again

            To your first question, change your insert statement to use NOW() as the value of the date.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              It is not possible (as far as I can tell) to use functions as a default values for fields in MySQl, so you will have to include this in your INSERT statement.
              You can use the NOW() function in the Insert statement, which will insert the current date into the field.

              If you don't like that you could create a procedure to do this, but thats a little much trouble for a simple thing like this, in my opinion.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                The one exception (I just discovered this today) is that you can use CURRENT_TIMESTA MP as the default value for the timestamp type.

                No, you can't use CURRENT_DATE as the default value for a date type. I don't know why.

                Comment

                Working...