There's probably a simple solution to this that will cause a facepalm. I have time stored as a 4 character long string ie 1300.
I'm trying to display that string as 13:00. I feel like there has to be a solution to this that is more elegant than what I'm doing at the moment.
I currently have:
$startTime = get_field($dayStart, $post->ID);
$endTime = get_field($dayEnd, $post->ID);
for ($x=0; $x = 4; $x++){
if(x == 2){
$ST .= ':';
$ET .= ':';
} else {
$ST .= $startTime[x];
$ET .= $endTime[x];
}
}
$startTime = $ST;
$endTime = $ET;
The string will always be 4 characters long.
Use concatenation to insert a character into a string at an index. To insert a character into a string at index i , split the string using the slicing syntax a_string[:i] and a_string[i:] . Between these two portions of the original string, use the concatenation operator + to insert the desired character.
One can use the StringBuffer class method namely the insert() method to add character to String at the given position. This method inserts the string representation of given data type at given position in StringBuffer. Syntax: str.
Insert a character at the beginning of the String using the + operator. Insert a character at the end of the String using the + operator.
$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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With