android6011 Posted March 7, 2008 Share Posted March 7, 2008 I'm just getting back into PHP and i feel stupid for asking, but I have a string that is YYYYddMM as a date, and I want to put a / inbetween the year, day, and month. what is the simplest way to do so? Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/ Share on other sites More sharing options...
rofl90 Posted March 7, 2008 Share Posted March 7, 2008 <?php echo date("Y/d/m") ?> If im wrong just escape the / with a \ Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486391 Share on other sites More sharing options...
android6011 Posted March 7, 2008 Author Share Posted March 7, 2008 im getting date from a file name, not the current date. I broke the date portion into a string so it looks like $date=yyyyddmm Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486392 Share on other sites More sharing options...
rofl90 Posted March 7, 2008 Share Posted March 7, 2008 you could use implode() Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486395 Share on other sites More sharing options...
android6011 Posted March 7, 2008 Author Share Posted March 7, 2008 THANK YOU. that is what i have been trying to remember for like 30 minutes, i kept searching but couldnt find the function name Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486396 Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 Before you can implode() you need the different parts separated in an array <?php $date='yyyyddmm'; $parts = sscanf ($date,'%4s%2s%2s'); echo implode ('/', $parts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486409 Share on other sites More sharing options...
Psycho Posted March 7, 2008 Share Posted March 7, 2008 Or use preg_replace: <?php $date='yyyymmdd'; echo preg_replace('/(.{4})(.{2})(.{2})/', '$1/$2/$3', $date); //output: yyyy/mm/dd ?> You can change the order of the parts my modifying the second parameter Quote Link to comment https://forums.phpfreaks.com/topic/94954-put-after-so-many-characters/#findComment-486442 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.