0

I have a html file (get from another website) with time number inside it, then i print it to my website, like this:

<?php
$html = file_get_contents('//google.com/ls.html');
echo $html;
?>

Content of ls.html is here : http://pastebin.com/xF6zyRiS

But problem is time in ls.html is UTC+1, so i need to convert them to another timezone, UTC+7.

I just tried with str_replace, like this:

<?php

// I need ':' because in ls.html have another number i do not want to change, like minute of the match or match result number
$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:");

$d_time = array();

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


$html = file_get_contents('//google.com/ls.html');

$result = str_replace(

    array($time[0], $time[1], $time[2], $time[3], $time[4], $time[5], $time[6], $time[7], $time[8], $time[9], $time[10], $time[11], $time[12], $time[13], $time[14], $time[15], $time[16], $time[17], $time[18], $time[19], $time[20], $time[21], $time[22], $time[23]),
    array($d_time[0], $d_time[1], $d_time[2], $d_time[3], $d_time[4], $d_time[5], $d_time[6], $d_time[7], $d_time[8], $d_time[9], $d_time[10], $d_time[11], $d_time[12], $d_time[13], $d_time[14], $d_time[15], $d_time[16], $d_time[17], $d_time[18], $d_time[19], $d_time[20], $d_time[21], $d_time[22], $d_time[23]),
    $html

);

echo $result;
?>

I expected this number will +6, like: 17: => 23:

But 17: will become 05:, because 17 replace to 23, then 23 replace to 05.

So question here is: how to tell str_replace stop when finish first replace, do not loop.

Or there is any better solution for change timezone.

Please help.

Tran
  • 185
  • 2
  • 4
  • 15
  • First you look in the [PHP manual for `str_replace()`](http://php.net/manual/en/function.str-replace.php) Then you see there is a fourth paramter. Then you read up on what that fourth parameter does, And then you attempt to implement it in your code – RiggsFolly Jan 19 '17 at 13:07
  • Of course timezone conversion can be done much easier by using the built-in features of DateTime etc – RiggsFolly Jan 19 '17 at 13:10
  • @RiggsFolly: you do not understand my situation, ls.html is html file i get from another website then print to my site, so it change very frequently, so i can not change it with Timezone conversion in php. I need to replace text. If html file is my file, i write it so i can change time with php but it not. – Tran Jan 19 '17 at 13:15
  • Answer to this question _how to tell str_replace stop when finish first replace, do not loop_ Is in my first comment – RiggsFolly Jan 19 '17 at 13:18
  • I do not understand what $count parameter will help here. And plase remove already has an answer header from my question please! – Tran Jan 19 '17 at 13:30

0 Answers0