Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 06-25-2008, 11:57 AM MySQL date format
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
The database requires the date to be entered in the format 2008-04-29 if the field datatype is date.

Can PHP be used to format this date to make it 29-04-2008 or 29th April 2008?

Or can you specify the format to store the date in the databse?
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-25-2008, 02:33 PM Re: MySQL date format
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
I don't know about MySQL formatting, but I can tell you how this works in SQL Server, and assume that it's fairly similar.

The date isn't actually stored in any particular format. It's stored as a series of byte values. I think it's about 1.5 bytes for the month and day, and 2.5 for the year, or 4 in all. (We have small and full date time types.) How you express these (MM-DD-YYYY or DD-MM-YYYY or YYYY-DD-MM or MM-DD-YY) actually isn't connected to how they're stored on disc. And for that reason, we're actually able to input them (using insert commands) in any particular format, so long as the server can tell what's what.

To get them formatted a particular way, our choices are limited. There are a few pre set ones that we can use the Convert function to get. Or we can use DatePart functions to extract the month, day, and year, then use string concatenation to put them back together. But for the most part, it doens't matter how they're formatted where SQL runs. Formatting is a function of consuming data, not processing it. So our ASP.NET code might query the database and fetch a list of values, including a date, and then format it in such and such a way while writing HTML all around it.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 06-25-2008, 10:47 PM Re: MySQL date format
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here is a simple function I wrote to convert MySQL dates to any format needed:
PHP Code:
function convert_date($date$format) {
    
$date strtotime($date);
    
$date date($format$date);
    return 
$date;

The $date parameter is the MySQL date, and $format is the desired output, formatted according the PHP's built in date() function, which you can read about here: http://php.net/date

To use this function for the second format you specified, you would do something like this:
PHP Code:
echo convert_date($mysqlDate"dS F Y"); 
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 06-25-2008 at 10:55 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-26-2008, 09:00 AM Re: MySQL date format
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Thanks for your replies, I managed to format the date using MySQL

PHP Code:
SELECT date_FORMAT(A.date'%M %e, %Y') as newDate
which prints out the date as:

April 29, 2008

Thanks,

Last edited by drew22299; 06-26-2008 at 12:55 PM..
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 06-26-2008, 11:12 AM Re: MySQL date format
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Interesting. I didn't know about that one.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to MySQL date format
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58550 seconds with 12 queries