PHP - Converting Hours to Mintues - php

I use this site alot and finally I have come across something that I can seem to figure out. I have a time returned of:
"1hr 59min"
What I would like it to do is covert this value to Minutes (119).
I havew tried various strtotime methods but none seem to do the trick. Any ideas?
cheers,
Stu

If it is a string you could do
$onefiftynine = "1hr 59min";
$split = explode(' ',$onefiftynine);
$hour = substr($split[0],0,strpos($split[0],'hr'));
$min = substr($split[1],0,strpos($split[1],'min'));
echo $hour * 60 + $min;

Read php docs and use right function, for example http://www.php.net/manual/en/datetime.createfromformat.php , etc.

If you use REGEX, then using preg_replace():
$str = '1hr 59min';
$str = preg_replace("/(\d+)hr (\d+)min/e", "$1*60+$2", $str);
// parsing the hour and minute, and doing calculation
If your input can have only hr or only min, then add the following two lines with above:
$str = preg_replace("/(\d+)hr/e", "$1*60", $str);
$str = preg_replace("/(\d+)min/", "$1", $str);

I guess you could use a regex :
function convertToSeconds($time)
{
$matches = array();
if (preg_match('/^(\d+)hr (\d+)min$/', $time, $matches) {
return $matches[1]*60 + $matches[2];
}
}

Related

Find a string that is not always the same [PHP]

I need help finding something in a variable that isn't always the same, and then put it in another variable.
I know that what I'm looking for has 5 slashes, it starts with steam://joingame/730/ and after the last slash there are 17 numbers.
Edit: It doesn't end with a slash, thats why I need to count 17 numbers after the fifth slash
Assuming what you're looking for looks something like this:
steam://joingame/730/11111111111111/
Then you could use explode() as a simple solution:
$gameId = explode('/', 'steam://joingame/730/11111111111111/');
var_dump($gameId[4]);
or you could use a regex as a more complex solution:
preg_match('|joingame/730/([0-9]+)|', 'steam://joingame/730/11111111111111/', $match);
var_dump($match[1]);
This splits the string into an array then return the last element as the game_id. It doesn't matter how many slashes. It will always return the last one.
$str = 'steam://joingame/730';
$arr = explode("/", $str) ;
$game_id = end($arr);
Following on from what DragonSpirit said
I modified there code so the string can look like
steam://joingame/730/11111111111111
or
steam://joingame/730/11111111111111/
$str = 'steam://joingame/730/11111111111111/';
$rstr = strrev( $str ); // reverses the string so it is now like /1111111111...
if($rstr[0] == "/") // checks if now first (was last ) character is a /
{
$nstr = substr($str, 0, -1); // if so it removes the /
}
else
{
$nstr = $str; // else it dont
}
$arr = explode("/", $nstr) ;
$game_id = end($arr);
Thanks for the help, I've found a solution for the problem. I'm going to post an uncommented version of the code on pastebin, becuase I couldn't get the code saple thing working here.
code

With PHP how do I extract a alpha numeric from a string

I've been searching this for awhile but I could not find the exact same case with mine.
With PHP I need to extract a 2 group of numbers separated with a period within a series of number and characters, it's always in between the last "-" and the last period.
Like: exten-651-652-20140423-154650-1398293210.14.wav to this 1398293210.14
Hope anyone can share their thoughts on this.
Thank you in advance.
it's always in between the last "-" and the last period.
$string = 'exten-651-652-20140423-154650-1398293210.14.wav';
$data = explode("-", $string);
echo str_replace('.wav', '', array_pop($data));
demo: https://eval.in/174006
Looks like it's a filename, so:
$basefile = pathinfo($string, PATHINFO_BASENAME);
$parts = explode('-', $basefile);
$desired_part = end($parts);

Php split a string to to string

I want to split a variable that I call for $ NowPlaying which contains the results of the current song. I would now like to share the following - so I get two new variables containing $ artist $ title. Having searched and tried to find a solution, but have stalled grateful for a little assistance, and help
<?php
// Assuming $NowPlaying is something like "J. Cole - Chaining Day"
// $array = explode("-", $NowPlaying); //enter a delimiter here, - is the example
$array = explode(" - ", $NowPlaying); //DJHell pointed out this is better
$artist = $array[0]; // J. Cole
$song = $array[1]; // Chaining Day
// Problems will arise if the delimiter is simply (-), if it is used in either
// the song or artist name.. ie ("Jay-Z - 99 Problems") so I advise against
// using - as the delimiter. You may be better off with :.: or some other string
?>
Sounds like you're wanting to use explode()
http://php.net/manual/en/function.explode.php
Use php explode() function
$str_array = explode(' - ', $you_song);
// then you can get the variables you want from the array
$artist = $str_array[index_of_artist_in_array];
$title = $str_array[index_of_title_in_array];
I would usually do some thing like this:
<?php
$input = 'Your - String';
$separator = ' - ';
$first_part = substr($input, 0, strpos($input, $separator));
$second_part = substr($input, (strpos($input, $separator) + strlen($separator)), strlen($input));
?>
I have looked at a couple split string questions and no one suggests using the php string functions. Is there a reason for this?
list() is made for exactly this purpose.
<?php
list($artist, $title) = explode(' - ', $NowPlaying);
?>
http://php.net/manual/en/function.list.php

Replacing a reoccurring value in a string with random values

Today is a sunny day
I would like to take the is and replace that with three random terms.
So: Today {was|wasn't|isn't} a sunny day
However, if is is in another string with five occurrences (say an article), I would like to replace each occurrence with a random value from {was|wasn't|isn't}
How can I accomplish this?
So far, I know you must use str_replace, with an array inside a foreach loop. However I can't get it working.
Any help with be greatly appreciated.
Thanks!
Try this:
$replacements = array("was", "wasn't", "isn't");
preg_replace("/\wis\w/e", "$replacements[array_rand($replacements)]", $text);
The 'e' modifier in the searched regular expression causes the replacement string to be evaluated as PHP code. array_rand is then used to pick a random key from $replacements
Alternative way...
$str = "Today is a sunny day";
$findme = "is";
$arr = array("was","wasn't","isn't");
$tmp = explode("is",$str);
$str = $tmp[0];
for($i=1;$i<count($tmp);$i++)
$str .= array_rand($arr) . $tmp[$i];
See this wack solution here
$d = array("was","wasn't","isn't");
$st = "Today is a sunny day, is it not?";
$arr = explode(" ", $st);
for($i=0;$i<count($arr);$i++){
if ($arr[$i] == "is"){
$r = rand(0, 2);
$arr[$i] = $d[$r];
}
}
foreach($arr as $v){
echo $v." ";
}
?>
Outputs
Today wasn't a sunny day, was it not?

Help on parsing formatted string

I have strings:
17s 283ms
48s 968ms
The string values are never the same and I want to extract the "second" value from it. In this case, the 17 and the 48.
I'm not very good with regex, so the workaround I did was this:
$str = "17s 283ms";
$split_str = explode(' ', $str);
foreach($split_str as $val){
if(strpos($val, 's') !== false) $sec = intval($val);
}
The problem is, the character 's' exists in both split_str[0] and split_str[1], so my $sec variable keeps obtaining 283, instead of 17.
Again, I'm not very good with regex, and I'm pretty sure regex is the way to go in this case. Please assist. Thanks.
You don't even need to use regex for this.
$seconds = substr($str, 0, strspn($str, '1234567890'));
The above solution will extract all the digits from the beginning of the string. Doesn't matter if the first non-digit character is "s", a space, or anything else.
But why bother?
You can even just cast $str to an int:
$seconds = (int)$str; // equivalent: intval($str)
See it in action.
Regular expressions are definite overkill for such a simple task. Don't use dynamite to drill holes in the wall.
You could do this like so:
preg_match('/(?<seconds>\d+)s\s*(?<milliseconds>\d+)ms/', $var, $matches);
print_r($matches);
If the string will always be formatted in this manner, you could simply use:
<?php
$timeString = '17s 283ms';
$seconds = substr($timeString, 0, strpos($timeString, 's'));
?>
Well, i guess that you can assume seconds always comes before milliseconds. No need for regexp if the format is consistent. This should do it:
$parts = explode(' ', $str);
$seconds = rtrim($parts[0], 's')
echo $seconds; // 17s
This will split the string by space and take the first part 17s. rtrim is then used to remove 's' and you're left with 17.
(\d+s) \d+ms
is the right regexp. Usage would be something like this:
$str = "17s 283ms";
$groups = array();
preg_match("/(\d+)s \d+ms/", $str, $groups);
Then, your number before ms would be $groups[1].

Categories