pwnuspoints Posted April 13, 2009 Share Posted April 13, 2009 Hey phpfreaks! I'm working on a simple php/sql blog. Up until now I have been using the php date() function to datestamp each entry in the database. I have been formatting the date as follows: date("F j, Y"); which is written to the 'VARCHAR' date column of my database as follows: "April 13, 2008" However, writing the date in this manner is clumbsy and forces me to display the date in the same format on every page. I guess my question is: Is there a smarter way to format the date and write it to my database? Your help and explaination is appriciated! Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/ Share on other sites More sharing options...
genericnumber1 Posted April 13, 2009 Share Posted April 13, 2009 Info on the mysql time datatypes: http://dev.mysql.com/doc/refman/5.1/en/datetime.html. Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808804 Share on other sites More sharing options...
ToonMariner Posted April 13, 2009 Share Posted April 13, 2009 yep - use the date data format for your fields in mysql - it makes searching/ordering by date more natural. retrieve that value and then you can manipulate it how you like with PHPs date/time functions Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808808 Share on other sites More sharing options...
Mchl Posted April 13, 2009 Share Posted April 13, 2009 Yes there is. You should store your dates as DATE format in MySQL. Then you should use either mysql's date and time functions, or PHP's date and strtotime (these two make my favourite combo, but there are other functions that can be used for that as well) Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808810 Share on other sites More sharing options...
pwnuspoints Posted April 13, 2009 Author Share Posted April 13, 2009 I'm already getting a better idea of how to handle dates, thanks everyone. As far as I gather, MYSQL understands dates written in 'YYYY-MM-DD' format. So, if i used the php date() function to write the date in that format it would look like this: date("Y-m-d"); Then I would be free to write that to my database normally and both PHP and MYSQL would 'understand' it?! right? right. I have a follow-up question-- When I create the 'date' column in my database table can it be set as VARCHAR? Or should it be set as something else? Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808829 Share on other sites More sharing options...
Mchl Posted April 13, 2009 Share Posted April 13, 2009 I have a follow-up question-- When I create the 'date' column in my database table can it be set as VARCHAR? Or should it be set as something else? Yes. It should be set to DATE. Quote Link to comment https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.