bchandel Posted August 26, 2009 Share Posted August 26, 2009 Is there any way to enter date in mySQL table directly in Month/Date/Year format automatically when a new record is added. At present when I am adding data directly in the mysql table it does not enter date by itself. I have to type it in 0000-00-00 (Year-Month-Day) format Is there any way to control this setting default properties for the date field in the table? Thank you! Link to comment https://forums.phpfreaks.com/topic/171903-solved-mysql-question-about-default-date-format-thank-you/ Share on other sites More sharing options...
LRSSS Posted August 26, 2009 Share Posted August 26, 2009 Use the date() function to format the current time or a timestamp http://us.php.net/manual/en/function.date.php I'm guessing you're looking for something along the lines of: date(Y-m-d) <?php echo date(Y-m-d); ?> Link to comment https://forums.phpfreaks.com/topic/171903-solved-mysql-question-about-default-date-format-thank-you/#findComment-906454 Share on other sites More sharing options...
bchandel Posted August 26, 2009 Author Share Posted August 26, 2009 What I am looking is when you design table in MySQL is there any way to set date field in a way that any time you enter data the current date is added in the form of m-d-Y. At present I have to enter the date in the record. I want this automatic process so when you create record date is entered in the date field in m-d-Y format. I hope I am able to clarify. Thank you! Link to comment https://forums.phpfreaks.com/topic/171903-solved-mysql-question-about-default-date-format-thank-you/#findComment-906458 Share on other sites More sharing options...
PFMaBiSmAd Posted August 26, 2009 Share Posted August 26, 2009 The YYYY-MM-DD format is not a "default" that can be changed, it is the format of a DATE data type. It exists for several reasons - it takes the minimum amount of storage, it has been optimized to result in the fastest queries, it can be directly sorted and used in greater-than/less-than comparisons, and a number of built-in mysql functions have been designed to use that format. You must convert your date values into the YYYY-MM-DD format when you insert your data and convert the YYYY-MM-DD format back to your format when you retrieve the data. There are several ways to do this, but the quickest is to use the mysql built-in functions - STR_TO_DATE(str,format) will allow you to put your date in as the str parameter (assuming you have validated that it is a correct date.) DATE_FORMAT(date,format) will allow you to retrieve a standard DATE data type in any format that you want. Link to comment https://forums.phpfreaks.com/topic/171903-solved-mysql-question-about-default-date-format-thank-you/#findComment-906463 Share on other sites More sharing options...
bchandel Posted August 26, 2009 Author Share Posted August 26, 2009 Thank youvery much for the detailed explanation. Link to comment https://forums.phpfreaks.com/topic/171903-solved-mysql-question-about-default-date-format-thank-you/#findComment-906474 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.