-2

I have a database linked upto a game. When the game insert's the players "playtime" it adds it like this for each player faction

playtime "[0,0,0]" (COP,MEDIC,CIV) - so each of the 0's would represent each player faction.

While the player is playing as that faction only 1 number will update which that number is for that faction.

Example playtime "[9012,1221,2663]" - so the player has now played for 44 Hours and 23 Minutes on CIV and 20 Hours and 21 Minutes on MEDIC and 150 Hours and 12 Minutes on COP

What I am trying to is display each number in a table, and show the time in table for each row ...

EXAMPLE

<table>
<tr>
<td>cop</td>
<td>medic</td>
<td>civ</td>
</tr>
<tr>
<td>150 Hours and 12 Minutes</td>
<td>20 Hours and 21 Minutes</td>
<td>44 Hours and 23 Minutes</td>
</tr>
</table>

So without displaying it in the time I've posted above, I am wanting to display each output as hour/minutes and in their respective table row.

I currently am using this

<td> " . str_replace( array('[',']', '"') , '' ,$row['playtime']). "</td>

which only shows the time like so 0,0,2891

If this does not make sense, please tell me.

MRTM4n
  • 1
  • 2
  • Explode the string, then take the parts and convert them into the proper time formats. – Adam Mar 22 '18 at 19:59
  • We are always glad to help and support new coders but *you need to help yourself first*. After [doing more research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) if you have a problem **post what you've tried** with a **clear explanation of what isn't** working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Read [How to Ask a good question](https://stackoverflow.com/help/how-to-ask). Be sure to [take the tour](https://stackoverflow.com/tour). – adprocas Mar 22 '18 at 20:04
  • https://stackoverflow.com/questions/1125730/how-can-i-split-a-comma-delimited-string-into-an-array-in-php – adprocas Mar 22 '18 at 20:06
  • @MRTM4n, did it when you tried? I'm assuming you've tried. I won't try it for you. Hence my response about helping yourself. – adprocas Mar 22 '18 at 20:09

3 Answers3

1

As you have $row['playtime'] in the form ["nnn,nnn,nnn"] first trim away square brakets and double quotes:

$playtime = trim( $row['playtime'], '"[]' );

Then split the string into an array with the three values with explode

$playtime = explode( ",", $playtime );

Use gmdate to convert seconds to H:m:s

$cop = gmdate( "H:i:s", $playtime[0] );
$med = gmdate( "H:i:s", $playtime[1] );
$civ = gmdate( "H:i:s", $playtime[2] );

You'll get each time in the format hh:mm:ss

You can go further with

$cop = explode( ":", $cop );
$cop = "{$cop[0]} Hours and {$cop[1]} Minutes";

And so on.

Paolo
  • 15,233
  • 27
  • 70
  • 91
0

This is simple but you should show your attempt. One way to achieve it would be like below (very vanilla PHP here):

$values = "9012,1221,2663";
$minutes = explode(',', $values);
foreach ($minutes as $item) {
    $hours = round(intval($item)/60);
    $minonly = gmp_div_r(intval($item),60);
    echo '<td>' . $hours . ' and ' . $minonly . ' minutes</td>' . PHP_EOL;
}
Paulo Hgo
  • 834
  • 1
  • 11
  • 26
  • My attempt was pretty much the same as the first example Here [link](https://secure.php.net/manual/en/function.explode.php) – MRTM4n Mar 22 '18 at 21:17
  • So it's not working yet? What's the exact format of your input string? I If it has brackets you can trim it out as suggested in another answer. – Paulo Hgo Mar 22 '18 at 21:27
0

You can use DateTime::createFromFormat() to create a DateTime object, and format your string using DateInterval created from diff():

// $row['playtime'] = '"[9012,1221,2663]"';
// here $row['playtime'] come from your data source

$times = explode(',', trim($row['playtime'], '[]"'));

$times = array_map(function($t) {
        $d1 = DateTime::createFromFormat("U", 0);
        $d2 = DateTime::createFromFormat("U", $t * 60);
        $diff = $d1->diff($d2);
        return ($diff->d * 24 + $diff->h) . " hours, " . $diff->i . " minutes" ;
    }, $times);

print_r($times);

Outputs:

Array
(
    [0] => 150 hours, 12 minutes
    [1] => 20 hours, 21 minutes
    [2] => 44 hours, 23 minutes
)
Syscall
  • 19,327
  • 10
  • 37
  • 52