How to insert any character in String

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
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

How to insert any character in String

Post 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
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

say $dbdate is the string you have

Code: Select all

$date = substr($dbdate,0,2)."-".substr($dbdate,2,2)."-".substr($dbdate,4);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: How to insert any character in String

Post 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)
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

yeah, tim is right, or make the fields DATE and use the mysql NOW() function to store them
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
Post Reply