Breaking & Fixing Dates from MySQL to PHP
The way that databases store "date" field data is:
2008-11-19 // year - month - day
The way that us humans read dates (at least in the U.S.) is:
11-19-2008 // month - day - year
I write a lot of administrative panel modules that either display dates or, better yet, allow customers to their own dates for articles, events, etc. I've created a couple of PHP functions that allow me to easily handle the transition from MySQL to PHP and visa versa.
The PHP: MySQL to PHP
/* break a date */ function break_date($date) { $date = explode('-',str_replace('/','-',$date)); return $date[1].'-'.$date[2].'-'.$date[0]; }
The PHP: PHP to MySQL
/* fix a date */ function fix_date($date) { $date = explode('-',str_replace('/','-',$date)); return $date[2].'-'.$date[0].'-'.$date[1]; }
You'll see that I automatically replace "/" within the given date, break apart the date string, and rearrange the items to make the user and the database happy. Hopefully this saves someone some time!
Why not use a more versatile function that accepts the format you wish as an extra argument?
Like this:
You can do stuff like:
cheers!
@Jochen: I’ll take a look at this — looks promising!
Question rephrased (sorry):
Why is the date in the middle of US time format? It makes no sense?
Year-Month-Date Hour:Minute:Second has a very logical order
You would not put the current time as 08:55:27 or 27:08 am?
Also as a more constructive post; you can use this method to get what you seek without even using php.
SELECT DATE_FORMAT(date_field, ‘%m %d, %Y, %l:%i%p’) as newdate FROM table_name;
Also as a more constructive post; you can use this method to get what you seek without even using php.
Also as a more constructive post; you can use this method to get what you seek without even using php.
SELECT DATE_FORMAT(date_field, ‘%m %d, %Y, %l:%i%p’) as newdate FROM table_name;
Also as a more constructive post; you can use this method to get what you seek without even using php.
Also as a more constructive post you can use this method to get what you seek without even using php.
I agree with riosatiy. And that’s exactly how we roll in the UK! :D (the date format)
Just to clarify, The reason dates are stored as 2008-11-19 is because you can sort them faster since they are already in the correct order. For example as an integer:
20081119
20081118
It is obvious which date preceded the other by simply subtracting the two integers.
11-19-2008
10-19-2009
When sorted these dates will get out of order.
With this in mind however like David is doing, always format the date for the humans!
@Mark: Yep. I’m not complaining about the Y-M-D format, but we simply cannot show users the date in that fashion.
And why can you not show dates in that fashion, exactly?
A better way to do this would be:
date('m-d-Y', strtotime($date));
I usually store dates/times in UNSIGNED INT fields, populated with time() on insert or update. After that, you cand format your INT (timestamp) however you like, just by passing the number as a second argument to the date function.
Agree with Patrick. A human can understand 2008-11-19 rather easily.
But when date formatting is needed… whats wrong with date()?
Unix timestamps… So much easier.
What happens when you have a date before the unix epoch? Anything before Jan 1, 1970 will store a value of 0. Use a signed column.
Thanks for pointing that out. I have made a mental note to take that into account next time I design a table with such a timestamp.
@All: One reason I don’t use the timestamp option is because I like to be able to read the date using PHPMyAdmin’s table view. It allows me to change something quick if I need to.
It’s also important to point our that if you need to store unix timestamp dates before ~Dec 1901 and after ~Jan 2038 then you should look into another method as that is the limit for 32-bit signed integer.
You could also just fetch the data from MySQL in the correct format in the first place. Let’s say you were fetching blog post headlines and dates:
SELECT title, DATE_FORMAT(posted_date, ‘%m-%d-%Y’) AS posted_at FROM blog_items;
Check out the date_format function at mysql.com.
Ah, I was about to point out the strtotime() function, but then I saw that Binny V A beat me to it.
I don’t use the Date field in MySQL.
I personally store the date in a INT field.
In that field, I put the value from the PHP Time function:
time();
And when I get it out of the database, I can output the date in any way I like, eg:
$date = date('Y m D', $dateValueFromDB);
This is the easiest way in my opinion.
I hope that made sense :)
In my experience it is best to process date formatting in SQL for a couple reasons. I’ve found it to be faster. My PHP code is cleaner. And I don’t need to worry about dates before 1970 if I use PHP native functions to format the date.
DATE_FORMAT(dateTime, ‘%M %d, %Y’) as formattedDate
Consult the MySQL docs (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format) for more output options.
Sorry David, I read your article fully. I understand now that this is to store user input into your DB and not pulling from the DB.
I would use regular expressions to do this in PHP. And stick with Perl style regular expressions because I’ve heard that PHP6 is supposed to discontinue ereg style.
Here is a sample that can be expanded much further to account for multiple formats:
This segment of code also formats the numbers so that something like
1-1-2008
will have$month = "01"
,$day = "01"
, and$year = "2008"
.Hope that helps, and sorry for any confusion.
I’ll weigh in and say that for most date related values I use an INT field and store the unix timestamp. It’s easier that way because I can store other templates for use in the date() for displaying the data in the browser.
I’m not sure how the date fields work but sorting on time and finding out if something occurred earlier is as is as using standard comparison operators.
I agree that the limitation is that it’s not easily read from a DB admin panel but I rarely have needed that functionality.
Why not?
SELECT DATE_FORMAT(date,’%m-%d-%Y’) AS date ….
Hi David,
The simplest way is the
strtotime
way.@Jay: Nice script, but you’re missing “)”‘s at the end of each strtotime() to close the date function.
In response to Mark’s comment, you might want to look at the ISO standards to see why dates are stored in the format they are:
Wikipedia article
@Jay is best one
Hi David,
This really helped me out. Thanks a lot!
Question:
First, I admit, I’m a newbie at PHP and mySQL so maybe this question is a bit too naive for this post. Just wondering how to go from the new MM-DD-YYYY format to something like “December 31, 07”
Would I do it in the function or inline in the output code somewhere? Thanks
@Doug:
I want to convert the date obtaining from the database(mysql) into another format(2009-09-18 into 18th Sep’09) using php. Please help me.
Thanking you in advance,
Nagendra.
@Nagendra: Use the date_format function in MySQL to fetch the date on a different format. For instance:
This would convert the YYYY-MM-DD column “date” into your wanted formatting.
I realize this is an older post, but I’d like to add that (since php5) there is also the DateTime class, which lets you format the date similar to php’s date function, like below, however I would suggest formatting date in Sql…
There’s another simple way to do that. I just posted something about it last week. The post itself in Hebrew, but you can still view the short code there and see..
First way is to select from db using DATE_FORMAT functions.
Second way is to format date with PHP on the returned date from DB.
Heres’ the link –
http://www.maorb.info/tech/2010-01/format-date-php-mysql
You may reply there in English if you want to.. :-)