Page 1 of 1

How to insert any character in String

Posted: Fri Oct 06, 2006 11:16 pm
by eshban
Hello,

I have a probelm in PHP. so plz help me.

In DB, i have an field called 'request_date'. Its type is 'varchar'
it gives result in that format:

27092006_1
27092006_2
28092006_1
28092006_2
28092006_3

here the values that comes before the underscore is the 'DATE' when that this record is inserted in DB.My requiement is that to show the current date,so for this i use explode function like

Code: Select all

$trip_post_date = explode("_",$rst_trip['request_date']);
after that i got date in that format:

27092006
27092006
28092006
28092006
28092006

Now to display proper date, i want to add dashes in date like
27-09-2006
27092006
28-09-2006
28-09-2006
28-09-2006

HOW CAN I ADD THESE DASHES. plz help me in this regard.

THanks
Eshban

Posted: Sat Oct 07, 2006 3:52 am
by Flamie
say $dbdate is the string you have

Code: Select all

$date = substr($dbdate,0,2)."-".substr($dbdate,2,2)."-".substr($dbdate,4);

Re: How to insert any character in String

Posted: Sat Oct 07, 2006 4:11 am
by timvw
eshban wrote: In DB, i have an field called 'request_date'. Its type is 'varchar'
it gives result in that format:

27092006_1
27092006_2
28092006_1
28092006_2
28092006_3
To me it seems your problem is actually the database design and not php :P

As already shown, you can use substr to get the day, month and year parts. If you need to do more processing i would strongly recommend to use mktime to convert this into a unixtimestamp. And from there you get access all the nice date and time functions php has to offer (btw, your database probably has string functions too, so you might want to write your query so that you only get the wanted data)

Posted: Sat Oct 07, 2006 4:13 am
by Flamie
yeah, tim is right, or make the fields DATE and use the mysql NOW() function to store them

Posted: Sat Oct 07, 2006 4:23 am
by daedalus__
timestamps ftw

TIMESTAMP field with the UNIX_TIMESTAMP function

in php just supply the timestamp to the date() function and it's beautfitult

Posted: Sun Oct 08, 2006 10:52 am
by Mordred
I've always feat uneasy about using a time system which will meet its limits in my expected lifetime :)
There is actually no reason that I can think of not to use DATE / DATETIME.