Search code examples
phpstringnumberssubstr

Add character after two character spaces from right to left


I have time stored in my database. Here is an example of a time:

800 or 1000

the first one is 8:00 and the second one is 10:00. I want to add a semicolon : after the second character starting to count from the right to left. how can i do this in php?

here is what i tried:

$realTime = substr_replace($oldtime,":", 2, -strlen($oldtime));

but it started from the left but i need it to start counting from the right. thanks.


Solution

  • Per the docs:

    If start is negative, the replacing will begin at the start'th character from the end of string.

    So use a negative number:

    $realTime = substr_replace($oldtime,":", -2, -strlen($oldtime));