Adding a character in the middle of a string

回眸只為那壹抹淺笑 提交于 2019-12-05 00:18:58
Ethan
$time = "1300";    
$time = substr($time,0,2).':'.substr($time,2,2);

Edit:

Here is a general solution to this problem:

function insertAtPosition($string, $insert, $position) {
    return implode($insert, str_split($string, $position));
}

I favor this solution as it is just one function

substr_replace('1300', ':', 2, 0);

http://php.net/substr_replace

implode(":",str_split($time,2));
arjentuin
substr_replace( $yourVar, ':', -2, 0 );

Will make 945 result in 9:45 and 1245 in 12:45.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!