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.

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.