Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a Date in PHP from MYSQL 1

Status
Not open for further replies.

rbaron

Programmer
Dec 14, 2002
6
US
Hello,

Thanks for looking at my post. I have been working at formating a date in PHP from a MYSql table. I am a newbie to MYSql and PHP so bare with me.

The date is stored in a field called cal_begin. The format it is stored in is 2003-08-18 which is far as I know is the only format PHP will store it in. I have written a php script which successfully connects to the database and extracts the field which it displays in the format as indicated above.

What I want to do is format the date like "August 18, 2003". So far I have tried a number of strings without success. The below string displays the date however as December 31, 1969 irrespective of what value is in the table column.

echo date('F d, Y','$cal_begin');

I have searched the PHP site as well as several books and cannot find an answer as to how to format the value of a date or time field. There is quite a bit of documentation on how to format a variable but not field output. Everything I have tried has failed.

If anyone can help me out on this it would be greatly appreciated.

Rick
 
It's not PHP that requires dates to be stored in YYYY-MM-DD format. It is MySQL that requires that.

Also, if you take a look at the online documentation on date() ( you will notice that when you pass date() a date-value, the value is required to be an integer, not a string.

Take the date from MySQL, run it through strtotime() ( which takes a date string and outputs it as an integer. Then use the output of that as the date value input of date():

echo date ('F d, Y', strtotime($cal_begin));

Want the best answers? Ask the best questions: TANSTAAFL!!
 
A thousand thank yous. I am really struggling with the syntax. I greatly appreciate your reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top