[SOLVED]MySQL NOW() to php date

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
mevets
Forum Newbie
Posts: 23
Joined: Fri Sep 15, 2006 10:06 am

[SOLVED]MySQL NOW() to php date

Post by mevets »

I am storing the time and date that users upload images. I do this by using NOW() in my mysql query and DATETIME as my datatype. I get dates like this:

Code: Select all

2007-08-21 21:16:06
When the user goes and looks at the time that they uploaded the image I dont want to make them look at that. Instead, I want it to look more like this:

Code: Select all

9:16 PM 8/21/07
Does anyone know how this should be done? I suspect I am adding the dates to the database incorrectly and should instead be using the number of seconds since the unix epoch, but I dont know how that is done. Articles on this particular circumstance are welcome as well!

Thanks!
Last edited by mevets on Wed Aug 22, 2007 2:28 pm, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I would suggest using the DATE_FORMAT MySQL function that ~Zoxive mentioned. However, if you want to get a unix timestamp out of that column, the easiest way would be to use the UNIX_TIMESTAMP MySQL function.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Be very careful when using strtotime if you ever use it.
PHP Manual wrote:The function expects to be given a string containing a US English date format
so 12/01/2007 is 1st December not 12th January as it should be in europe (rest of the world ?). I've run afoul of it in the past.

While I'm here the postgres version to get the php time is "select date_part('epoch',now())" in case anyone is looking at this thread and wondering how to do it (despite the MYSQL in the topic title);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Either modify the date format at the database server or return a Unix time stamp from the database server and manipulate it in PHP. I would go with the first choice rather than the second in this case.
Post Reply