can i use str_replace for this?

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

can i use str_replace for this?

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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?)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ok, thank you very much

i will try that out now
Post Reply