mayfair Posted September 17, 2009 Share Posted September 17, 2009 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, - 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. Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 17, 2009 Share Posted September 17, 2009 $delivery_date = preg_replace('~(.*?)/([0-9]{2})$~', '$120$2', $delivery_date); Quote Link to comment Share on other sites More sharing options...
trq Posted September 17, 2009 Share Posted September 17, 2009 Is this all that is in the string? Quote Link to comment Share on other sites More sharing options...
mayfair Posted September 17, 2009 Author Share Posted September 17, 2009 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' Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 17, 2009 Share Posted September 17, 2009 Sorry, forgot to add the start anchor $delivery_date = preg_replace('~^(.*?)/([0-9]{2})$~', '$120$2', $delivery_date); [code] Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 17, 2009 Share Posted September 17, 2009 Damnit, I also forgot a slash in the replacement $delivery_date = preg_replace('~^(.*?)/([0-9]{2})$~', '$1/20$2', $delivery_date); Quote Link to comment Share on other sites More sharing options...
mayfair Posted September 17, 2009 Author Share Posted September 17, 2009 Damnit, I also forgot a slash in the replacement Yep, that's cracked it. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.