Extracting month, day, year from a date field in MySQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
korngold
Forum Newbie
Posts: 11
Joined: Wed Nov 02, 2005 11:49 am

Extracting month, day, year from a date field in MySQL DB

Post by korngold »

Hi All,

This is probably an easy one, but I just need to extract the numeric value for the month, day, and year of a MySQL Date/Time field. I'm an ASP convert, and in ASP you could simply use month(date), day(date), or year(date). Are there similar functions in PHP? I did google & search the php online manual and came up dry.

Thanks in advance!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are multiple ways. MySQL can perform the such extractions via the EXTRACT() function, or you can use PHP's functions. The function to use from PHP depends on how the information from MySQL is returned. If a DATE/DATETIME type, explode() can be used to break the resultant string apart into array elements; When using a Unix timestamp compatible field (INT), the date() function can be used.
smartknightinc
Forum Newbie
Posts: 4
Joined: Wed Nov 02, 2005 11:45 pm

solution

Post by smartknightinc »

hi,

Use date_format() function of mysql

:)
Thanks & Regards
harry
korngold
Forum Newbie
Posts: 11
Joined: Wed Nov 02, 2005 11:49 am

Post by korngold »

Worked like a charm! Thank you!!!
Post Reply