Datetime format

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
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Datetime format

Post by johnperkins21 »

I have a mySQL datetime field that I am populating with the SQL now() funtion. The result I get when I pull that out of the database is something like:

2004-03-16 09:58:03

What I want is something like:

Mar 16, 9:58 am


I have looked at date() and strtotime() and mySQL's date_format, but for some reason I am getting very confused. I don't need the code to do it, but an idea of which one of these I should use to do it would help me greatly. That way I can take it and play around with it until I get it working the way I want.

Thanks,
John
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i would use the date() function. Then insert it into the database
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

If I use the date() function, would my searches still work properly if I sorted by date? I'm using this for a shoutbox, so I like using the now() function for putting it into the database, I just want to change the output. I'm sure that I could use preg_replace somehow, but that looks even more confusing than any of the date functions.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

... one question.. why sort by date?? why not have an auto increment field and sort by that?? Thats the way its usually done...
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Umm, because I'm a moron. That's an excellent point. Thanks for your help.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

lol, anytime!! lol
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

I would insert the date as a database date yyyy-mm-dd and then convert it to whatever format after it's been retrieved, so i can use SQL time/date functions when i do queries. Then to sort it i would just put an 'order by date' at the end of the query
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

your just a little late andre_c, i think he already has a solution....??did you read any of the above posts??
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

I did, but I thought i would give my opinion on a different way of doing it, should i have not done that? sorry, if so.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

andre_c, different methods are always welcome, but I will invariably take Illusionists suggestion as the SQL time/date functions have me completely baffled. At least PHP's date() function has a little bit of info in the manual I can understand.

Thanks for your input.
Post Reply