mass add time in a file
i have a file in which time appears nearly hundred times like
00:01:32
00:01:33
00:01:36
.......................
how c开发者_如何学Can i add 2 seconds or 2 minutes to all the times in the file so that i get
00:01:34
00:01:35
00:01:38
..................
You can use preg_replace
with the e
modifier to make it execute code to determine the replacement, then strtotime
can parse it and do the necessary adjustments:
$data = preg_replace('/([0-9]{2}(:[0-9]{2}){2})/e', 'modify_time("$1")', $data);
function modify_time($time) {
return date('H:i:s', strtotime('+2 seconds', strtotime($time)));
}
Actually it's elementary school question, how to add some minutes to the certain time. this text can be easily parsed into array with seconds and then adding certain amount of seconds to it's each member wouldnt be a big deal.
$min=date("i")+2;
if ($min<10)
{
$min="0".$min;
}
$time= date("H:").$min;
echo $time;
<?php
$fp = fopen('yourfile.txt', 'r');
$fpr=fopen('result.txt','a');
while (!feof($fp))
{
$mytext = fgets($fp, 9);
echo $mytext."<br />";
$result = substr($mytext,-2,2);
if ($result<=60)
{
$result=$result+2;
$result=substr($mytext,1,5).$result;
echo $result."<br>";
fwrite($fpr, $result);
}
else
{
if ($result==58)
$result="00";
if ($result==59)
$result="01";
if ($result==60)
$result="02";
$result=substr($mytext,1,5).$result;
echo $result."<br>";
fwrite($fpr, $result);
}
}
?>
精彩评论