Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808810
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/153889-solved-date-phpsql/#findComment-808829
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.