Jump to content

Date formatting between PHP and MySQL


wayjo

Recommended Posts

Hi folks,

 

I am a bit of a newbie with PHP and MySQL but I am thoroughly enjoying it. I have a dilemma which I am sure is only so because of my own ignorance. here's the problem...

 

I have a database that basically helps manage a list of tasks. When I write a new task to the task_master table I automatically use the MySQL DATETIME field format, eg "YYYY-MM-DD HH:MM:SS". Now, I do want all this information but I do not want to display (initially) all of it, I just want the "YYYY-MM-DD" part, equivalent to MySQLs DATE format.

 

I am trying to find a date function in PHP that will take the MySQL stored date and time and allow me to just echo the year, month and day.

 

Should I not use the MySQL format and just use PHP to create a date/time it will understand an stick it in text field or can I do both?

 

I get a feeling I am missing something really obvious so please accept my apologies in advance. Darn newbies, nothing but a nuisance !  ;D

 

Thanks in advance for any assistance that can be offered.

 

cheers

 

wayjo

Link to comment
https://forums.phpfreaks.com/topic/120704-date-formatting-between-php-and-mysql/
Share on other sites

You can do that just by using DATE_FORMAT in your MYSQL query wayjo.

 

something like...

 

$query = "SELECT *, DATE_FORMAT(followup, '%D %b %Y') AS followup FROM contacts WHERE contacts_id = $record";

 

You want this part (where followup is the name of your datetime column in mysql) -

DATE_FORMAT(followup, '%D %b %Y') AS followup

 

If you want to view all the different ways of formatting have a look at http://www.dan.co.uk/mysql-date-format/

 

Regards

 

James

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.