views:

28

answers:

3

I have a date variable with the format

2008 12 29

to have it correctly display from within my database app I need the format to be

2008-12-29

Is there a way to simply add the - into the string or replace the spaces with -?

I am using PHP and the date is stored in $release_date

+3  A: 

Use str_replace():

$release_date = str_replace(' ', '-', $release_date);
BoltClock
A: 

The str_replace() method is what you search for:

$good_format_date = str_replace(' ', '-', $date);
Peter Smit
A: 

If you know for a fact that the spaces will always be standard (spacebar) spaces, use str_replace() as BoltClock said.

However, if it's possible that there may be extra spaces, tabs, or other whitespace characters in between your date parts, use preg_replace() as it will work in almost all cases unlike str_replace():

$release_date = preg_replace( '/\s+/', '-', $release_date );
smekosh