I have a string, for example
20120201
I need to change it to
2012/02/01
obviously I need to add "/" after the 4th, 6th characters in the string.
Could someone help me do this in PHP?
Thank you.
I have a string, for example
20120201
I need to change it to
2012/02/01
obviously I need to add "/" after the 4th, 6th characters in the string.
Could someone help me do this in PHP?
Thank you.
Something like this:
$final = substr($initial,0,4).'/'.substr($initial,4,2).'/'.substr($initial,6,2)
Use substr.
,2 is not required as substr will go to the end if the length isn't givenYou could use substr, something like
$result = substr($str,0,4)."/".substr($str,4,2)."/".substr($str, 6)
edit: Messing between Java and Php for String concat :p
If you want inserting, you can use http://php.net/substr_replace
$string = "20120201";
$newString = substr_replace(substr_replace($string, "/", 6, 0), "/", 4, 0);
or concatenate substrings got by http://php.net/substr
$newString = substr($string, 0, 4) . "/" . substr($string, 4, 2) . "/" . substr($string, 6)
You could use date_parse_from_format to parse the date you have there into a PHP date object, then you could output it in any format you'd like. A list of available date formats is located here