0

In text, i have a lot of time number, then i want to change hour to another timezone (+6), example :

00:15 => 06:15

01:00 => 07:00

... and so on. I'm trying this :

$result = str_replace(
  array("00:","01:","02:","03:","04:","05:","06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:"),
    array("06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:","00:","01:","02:","03:","04:", "05:"),
    $text
);
 echo $result;

But 18: will replace with 04: because php replace 18: to 22: then continue replace 22: to 04:

How to solved this, thank you.

// Edit : To @user3414969 and @Medda86: $text is the data i'm get from another site, that mean i can not control the source, only way to do is replace

// Edit 2 : Here is content : http://bongdatv.net/test.php

// Edit 3: Please solve this problem with replace way, not calculation number way.

1
  • you just need to add 6 hour in time right Commented Dec 28, 2015 at 10:27

4 Answers 4

1

I think best is to use the timestamp format, add the time and get out the new time from that.

http://php.net/manual/en/function.time.php

Sign up to request clarification or add additional context in comments.

2 Comments

I just not control $text because i'm get data from another site.
just loop the array and run the time function on each value :)
1

$time = array("00:","01:","02:","03:","04:","05:","06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:");

$required_time = array();
    
foreach($time as $t){
    $hour = $t."00"; // 00 appending 0 minites
    $hours_plus = 6; // adding 6 hours
    $required_time[] = date('H:', strtotime($hour)+($hours_plus*60*60));
}
echo "<pre>";
    print_r($required_time);
    echo "</pre>";

3 Comments

Result:( [0] => 06: [1] => 07: [2] => 08: [3] => 09: [4] => 10: [5] => 11: [6] => 12: [7] => 13: [8] => 14: [9] => 15: [10] => 16: [11] => 17: [12] => 18: [13] => 19: [14] => 20: [15] => 21: [16] => 22: [17] => 23: [18] => 00: [19] => 01: [20] => 02: [21] => 03: [22] => 04: [23] => 05: )
how to apply to my code, just try : $result = str_replace($time, $required_time, $text); echo $result; but not work. Sorry i'm bad with php.
str_replace is for string, not for array. That's why I used foreach loop to generate your required result
0

Optimal way is as suggested by Medda86
However, you can try upon this way

$array = ("00:","01:",....);
//Then you can loop over array and add the time 
for($i=0 ; $i < sizeof($array);$i++){
       $array[$i] = intval($array[$i]+6)%24;
       if($array[$i] < 10)
         $array[$i] = str_pad($array[$i],2,'0',STR_PAD_LEFT).':';
       else
         $array[$i] .= ':';
}

3 Comments

I need 00:, not 0. Because in $text have another number that i do not to change.
Please look at URL : bongdatv.net/test.php I need change time match start, not score number.
for the above code foll $array is :array(24) { [0]=> string(3) "06:" [1]=> string(3) "07:" [2]=> string(3) "08:" [3]=> string(3) "09:" [4]=> string(3) "10:" [5]=> string(3) "11:" [6]=> string(3) "12:" [7]=> string(3) "13:" [8]=> string(3) "14:" [9]=> string(3) "15:" [10]=> string(3) "16:" [11]=> string(3) "17:" [12]=> string(3) "18:" [13]=> string(3) "19:" [14]=> string(3) "20:" [15]=> string(3) "21:" [16]=> string(3) "22:" [17]=> string(3) "23:" [18]=> string(3) "00:" [19]=> string(3) "01:" [20]=> string(3) "02:" [21]=> string(3) "03:" [22]=> string(3) "04:" [23]=> string(3) "05:" }
0

Try this:

$yourArr = array('00:15','01:00','00:30');
foreach ($yourArr as $key => $value) {
    $timestamp = strtotime($value) + 60*60*6; // add hours as per your need.
    $time = date('H:i', $timestamp);
    $newArr[] = $time;
}
echo "<pre>";
print_r($newArr);

Result is:

Array
(
    [0] => 06:15
    [1] => 07:00
    [2] => 06:30
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.