Crew-Portal Posted November 17, 2007 Share Posted November 17, 2007 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... Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 <?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; ?> Orio. Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 17, 2007 Author Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 Yep Orio. Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 17, 2007 Share Posted November 17, 2007 Hmm...would this also work? Just curious... $res = array(); $tags = array("D","H","M"); str_replace($tags," ",$string); $res = explode(" ",$string); Don't forget to click "TOPIC SOLVED" Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted November 17, 2007 Author Share Posted November 17, 2007 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! Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 17, 2007 Share Posted November 17, 2007 toplay's would catch both one digit and two digit results. Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 Using str_* functions is always more efficient than using regex. At least that what the manual suggests. Orio. Quote Link to comment Share on other sites More sharing options...
toplay Posted November 17, 2007 Share Posted November 17, 2007 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.