Jump to content

[SOLVED] Add text to string at a certain position


mayfair

Recommended Posts

Hello Guys

 

I have a string which contains a date formatted to look, for example, like: Thursday, 13/09/09

 

This is used throughout my site in various places and works great, however I now need to integrate this with an older ordering system which only accepts dates in the format of dd/mm/yyyy. I can strip the day of the week by using substr($delivery_date, -8) which gives me the equivalent of 13/09/09, however the year needs to be in 4-digit format and I can't seem to find a function that will allow me to add '20' to the beginning of the year. Using str_replace obviously overwrites the end portion of the date instead of adding to it, is there a simple workaround for this?

 

The date will always remain a fixed length of 8 characters which should be in my favour. Any help would be greatly appreciated.

Is this all that is in the string?

 

Yes, after it has been trimmed the string contains exactly 8 characters like: 17/09/09

 

$delivery_date = preg_replace('~(.*?)/([0-9]{2})$~', '$120$2', $delivery_date);

 

Sorry Garethp, this didn't seem to work. When I ran it on: '27/10/09', I got: '009'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.