Jump to content

[SOLVED] calculating time?


Crew-Portal

Recommended Posts

Okay I got a small delema for you guys. When I get time from a video game it has the time syntax like 0D 0H 0M and it can hold data up to 23D 23H 59M. So how would I take that type of syntax and take only the H and M time variables and put it into a database called Minutes & Hours. Dont worry about the Day datatype because no-one will spend 1 day in a game unless thier idleing which would be cheating...

 

So take 0D 14H 42M and put it in 2 colums in a database the one bing Hours And THe Other Being minutes. I think theres an explode and implode statements in ther but I have no idea on how to do it! Thanks in advance...

Link to comment
Share on other sites

sorry for being dumb but the syntax

<?php

$date = "23D 23H 59M";

list($d,h,m) = explode(" ", $date);
$d = rtrim($d, "D");
$h = rtrim($h, "H");
$m = rtrim($m, "M");

echo $d." ".$h." ".$m;

?>

 

Well the echo statement will be 23 23 59

and I can insert the hour into colum hour with the variable $h and minutes with the variable $m? I think Im right but just to make sure I understand fully.

Link to comment
Share on other sites

It's easier to use PCRE. Example:

 

<?php

$strData = '0D 14H 42M';

if (preg_match('/(\d{1,2})H.+?(\d{1,2})M/i', $strData, $arrMatches)) {
$intHours   = $arrMatches[1];
$intMinutes = $arrMatches[2];
} else { // No match
$intHours   = 0;
$intMinutes = 0;
}

echo 'Hours: ', $intHours;     // Will display 14
echo 'Minutes: ', $intMinutes; // Will display 42

?>

 

Link to comment
Share on other sites

Ya the only thing is that there can either be 1 or 2 numbers in each variable like it can be 1 hour or 11 hours not 01 and 11. Thats why I think the first one will work the best!

 

The "{1,2}" notation in the PCRE I posted handles what you mention above.

 

It's sometimes best to extract data than to strip away data, which is what my code does (extract the hours and minutes you need).

 

EDIT:

 

Dragen understands. :)

 

Link to comment
Share on other sites

Using str_* functions is always more efficient than using regex. At least that what the manual suggests.

 

Orio.

 

Yes, agreed, however, sometimes calling one PCRE function is better than having several lines of code that call several functions (which may not be in the end that efficient either).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.