![]() |
![]() |
#1 (permalink) |
Psycho
Location: Orlando, FL
|
[PHP/MySQL] formatting the date
I'm still just learning PHP and MySQL and I'm having a problem understanding how to print out the date of a table entry. I can print it out so that it looks like this:
20040712112214 ....but how do I format it so that it makes sense? I know there is a DATE_FORMAT function in MySQL, but I guess I don't know how to use it cause I can't get it to work. |
![]() |
![]() |
#2 (permalink) |
Junkie
|
Always store your timestamps as a Unix date integer and use the PHP date() function to format it as human-readable text.
http://us2.php.net/manual/en/function.date.php |
![]() |
![]() |
#3 (permalink) |
Psycho
Location: Orlando, FL
|
The type of field I have my date/time setup as is TIMESTAMP. When I'm defining and running my query, I'm doing it like this:
$query = 'SELECT * FROM weblog2 ORDER BY entrydate DESC' if ($r = mysql_query ($query)) while ($row = mysql_fetch_array ($r)) { print ... How do I go about using the PHP date() function? Where do I go from here? |
![]() |
![]() |
#4 (permalink) |
Crazy
Location: Salt Town, UT
|
SinisterMotives: I thought I was the only one that knew that secret... now it's out of the bag, everybody is going to be able to do time easily.... now if they just could not learn about setting the locale and printing the date normally to adjust for timezone settings, I could have cornered the market.
FaderMonkey: A great way to handle TIMESTAMP columns is either to alter them into INT columns that are unix timestamps, or if that isn't an option, on each and every select and insert, you can use UNIX_TIMESTAMP(column_name) (for selects) and FROM_UNIXTIME(timestamp) (for inserts/updates). MySQL's date/time functions are really lacking, so I normally just avoid them and use PHP all of the way. It's a lot easier, but still harder than Postgres's full timezone support for it's date/time columns. |
![]() |
![]() |
#5 (permalink) | |
Junkie
|
Quote:
To answer your question - how to use the date() function: the first argument to date() is a string built up of the flags shown in the table on the manual page I linked you to. Example: PHP Code:
Code:
The Unix representation of the current date and time is 1089736421 July 13, 2004, 10:33 am |
|
![]() |
![]() |
#6 (permalink) | |
Psycho
Location: Orlando, FL
|
Quote:
PHP Code:
|
|
![]() |
![]() |
#7 (permalink) |
Junkie
|
You'll probably have to replace the asterisk with the actual names of the fields:
PHP Code:
|
![]() |
![]() |
#9 (permalink) |
Psycho
Location: Orlando, FL
|
Oops...I guess I was wrong. The date() function is printing the current time instead of the timestamp from the data base. Here's how I wrote it:
PHP Code:
|
![]() |
![]() |
#12 (permalink) |
Psycho
Location: Orlando, FL
|
When I do this:
PHP Code:
When I do this: PHP Code:
![]() |
![]() |
![]() |
#13 (permalink) |
Fluxing wildly...
Location: Auckland, New Zealand
|
In that first one there you have a { before $row['entrydate'] which is causing the parse error.
__________________
flux (n.) Medicine. The discharge of large quantities of fluid material from the body, especially the discharge of watery feces from the intestines. |
![]() |
![]() |
#17 (permalink) |
Psycho
Location: Orlando, FL
|
I ended up doing it this way and it seems to be working:
PHP Code:
|
![]() |
Tags |
date, formatting, php or mysql |
|
|