PHP date formatting ass backwards Sep16 '05
Why is PHP date formatting so "ass-backwards?"
I love the simplicity of the PHP programming language - but when it comes to dates and times, things are very long-winded.
All I want to do is format an SQL date/time string, such as:
2005-09-16 07:33:41
This date/time string is how all date/time strings are stored in MySQL database tables. This is the default order:
YEAR-MONTH-DAY hour:minute:second.
Sure, I could just leave the date/time like that, but it’s much more intuitive to format the date/time string, so it doesn’t look so much like "computer code." Also, some file types (XML, for example) only accept certain date/time formats, in order to be parsed correctly.
Here are some different ways to format the date/time string:
Friday, September 16, 2005 - 7:33 AM 9/16/05, 7:33 AM CST 16 Sep 05, 7:33 AM
The possibilities are only limited to the imagination.
Except... PHP makes it very difficult to go from "computer date/time" to "intuitive date/time."
For example, PHP has a date() function, which formats the date/time string - but only for the current date/time.
What I need to do is format a specified date/time - for blog posts, comments, etc. This shouldn’t be that complicated.
That PHP date() function can format a specified date/time, but only with a major "work-around," which includes the use of another function - mktime(), which takes "ass-backward" parameters.
mktime() asks you to include (in this order) the following values: hour, minute, second, month, day, year.
So, a call to the mktime() function could look like this:
mktime(07, 33, 41, 09, 16, 2005);
This information has to be extracted from the SQL date/time value: 2005-09-16 07:33:41.
Also, the mktime() function won’t accept those trailing zeros - as in 09, or 07. They have to be 9, or 7.
-----
This rant is brought to you by an entire day wasted, trying to complete a simple task.
Add Feedback (view all)
Leave feedback
Dude, this is simple to do! Just do it with SQL and not PHP SELECT DATEFORMAT(postdate, '%a %M %D, %Y') as post_date ... Read more.
I used this string posted by Someone, and it worked perfect: $formattedDate = date("l, jS F Y", strtotime($dateValue)); Thanks! ... Read more.
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Popular Pages
- Fast rounded corners in Photoshop (3954 recent visits)
- PHP – passing variables across pages (1485 recent visits)
- JavaScript set selected on load (1208 recent visits)
- Removing all child nodes from an element (827 recent visits)
- iPod songs out of order? (719 recent visits)
- Britney - Everytime piano tab (649 recent visits)
- Firefox 3 smart address bar: wildcard search (607 recent visits)
- MySQL LEFT JOIN syntax (512 recent visits)
- Breathe Me - Sia (501 recent visits)
- Tumblr: how blogging should be (384 recent visits)
Similar Entries
- PHP: Skipping index page call in URL (19 recent visits)
- PHP project: convert times to numbers (54 recent visits)
- PHP – passing variables across pages (1485 recent visits)
- Install Apache, PHP, MySQL on Windows (17 recent visits)
- Code mnemonics: PHP implode/explode (52 recent visits)
- Swap banner image with CSS and PHP (110 recent visits)
Stats
23 unique visits since August 2008
Recent Referrers (click)
- formatting a date php
- php date formatting
- php date formatting
- php date formatting
- php date formatting
- Formatting date + PHP
- php date backwards
- php date formatting
- php date formatting
- php date formatting ago
- php read string backwards
- PHP dates to SQL dates
- php date formatting
- php date formatting
- php formatting sql date
- dates trailing zero php
- set date backward php
- string
- format a date seconds ago php
- PHP Date Formatting
$formattedDate = date("l, jS F Y", strtotime($dateValue)); ... Read more.