Page 1 of 1

can i use str_replace for this?

Posted: Wed Mar 03, 2004 5:55 am
by malcolmboston
hi guys

anyway straight to the point

basically i have a nice little section of my site that sorts by last logged, last activity and register date, i used teh mysql now() function to do this so i get the date in the format (something like it anyway)
mysql wrote: 2004-03-03 15:23:23
now i personally dont mind it like that as it guarantees mysql will be able to 'sort' with it, however i am making this information viewable by the public also so i was wondering if i could replace it with something like 3rd March 3004 at 15:23:23

what function should i use?

and what would the basic coding look like as i havent used any replace functions before (never need to)

thanks in advence

Posted: Wed Mar 03, 2004 5:59 am
by twigletmac
Use MySQL to do this - look for DATE_FORMAT() on this page:
http://www.mysql.com/doc/en/Date_and_ti ... tions.html

Best and easiest way.

Mac

Posted: Wed Mar 03, 2004 5:59 am
by JayBird
would be nicer to do it in your query like this

Code: Select all

SELECT *, DATE_FORMAT(last_login, '%d-%m-%Y') AS login_date, FROM users
Change '%d-%m-%Y' to suit your needs

Mark

Posted: Wed Mar 03, 2004 6:04 am
by malcolmboston
ok admittedly i havent read the whole of twig's link yet

however the way i though it would work (or i would do it)

create a seperate script for creating a new 'format' of the original string and echo the new string for the users, the cms i have built will still have the now() format (is much better me thinks)

would i be able to do it this way (have 2 seperate instances?)

Posted: Wed Mar 03, 2004 6:07 am
by JayBird
it makes no sense to do it that way, why not just format it as you are selecting it. It still stays thie same in the DB.

Mark

Posted: Wed Mar 03, 2004 6:08 am
by twigletmac
Check out Bech100's SQL statement example - basically you make the formatting change in the SELECT statement that you would use anyway to retrieve the data. That means that you store the date and time in a proper DATETIME field but that when you want to display it you can format it any way you like.

Mac

Posted: Wed Mar 03, 2004 6:08 am
by malcolmboston
ok, thank you very much

i will try that out now