I have these strings as $remaining_time:

"3h5m"
"4h9m"
"15m"
"17h"
"6d17h"

What I want to do is be able to parse through them and output the results in unix format so I can add them up to my $start_time (in unix timestamp) to get the end time.

Any suggestions how I can do this?

Thanks.

John

Recommended Answers

All 4 Replies

$h=0;$m=0;$d=0;
$mystring= "47h9m10d";
$d=getValue($mystring,"d");
$h=getValue($mystring,"h");
$m=getValue($mystring,"m");

echo "d:".$d." , h:".$h.", m:".$m;	
function getValue($mystring,$type)
{
	$pos = strpos($mystring, $type);
	if ($pos !== false)
	{
	 	$v=substr($mystring, $pos-2, 1);
	 	if(is_numeric($v))
	 		$v=substr($mystring, $pos-2, 2);
	 	else
	 		$v=substr($mystring, $pos-1, 1);
	}
	return $v;
}

But this works only for 2 digit values like '99h99m99d' not for '100d100m100h'.
For this change the above code accordingly.

Thanks for that!

Here's what I did:

function get_time_left($time_left_string)
{
	$last_symbol = 0;
	$num = "";
	$result = 0;
	
	//make time_remaining into an array
	$time_left_array = str_split($time_left_string);
	
	//iterate through them until a 'd', 'h' or 'm' is found
	for ($i = 0 ; $i < count($time_left_array) ; $i++)
	{
		$multiplier = 1;
		if (!is_numeric ($time_left_array[$i] ))
		{
			$last_symbol = $i + 1;
			
			if ($time_left_array[$i] == 'd')
				$multiplier = 86400; //1 day = 86400 seconds
			else if ($time_left_array[$i] == 'h')
				$multiplier = 3600;  //1 hour = 3600 seconds
			else if ($time_left_array[$i] == 'm')
				$multiplier = 60;    //1 minute = 60 seconds
			
			$num = (int)$num * $multiplier;
			
			$result += $num;
			$num = "";
		}
		else $num .= $time_left_array[$i];
	}
	
	return $result;
}

Tell me what you think about this.^^

Regards,

John

The strtotime function is really good at calculating time from strings. See the documentation: http://php.net/manual/en/function.strtotime.php

Following the example they have:

echo strtotime("+1 week 2 days 4 hours 2 seconds");

We can convert your string to one that strtotime can understand by simple replacements of "d" with "day", "h" with "hour", "m" with minute and "s" with "second" etc.

eg:

$time_left_str = '2d24h';

// search and replace these so strtotime understands the string
$search = array('d', 'h', 'm', 's');
$repl = array(' day ', ' hour ', ' minute ', ' second ');
// calculate future time from string
$future_time = strtotime(str_replace($search, $repl, '+' . $time_left_str));
// calculate time left
$time_left_secs = $future_time - time();

* Note this should be sufficient for time() but since time() is calculated in two different operations it does not have exact precision. In order to do that you need to calculate time in one operation. So if you were to use microtime() for instance, you could be a few microseconds off..

The strtotime function is really good at calculating time from strings. See the documentation: http://php.net/manual/en/function.strtotime.php

Following the example they have:

echo strtotime("+1 week 2 days 4 hours 2 seconds");

We can convert your string to one that strtotime can understand by simple replacements of "d" with "day", "h" with "hour", "m" with minute and "s" with "second" etc.

eg:

$time_left_str = '2d24h';

// search and replace these so strtotime understands the string
$search = array('d', 'h', 'm', 's');
$repl = array(' day ', ' hour ', ' minute ', ' second ');
// calculate future time from string
$future_time = strtotime(str_replace($search, $repl, '+' . $time_left_str));
// calculate time left
$time_left_secs = $future_time - time();

* Note this should be sufficient for time() but since time() is calculated in two different operations it does not have exact precision. In order to do that you need to calculate time in one operation. So if you were to use microtime() for instance, you could be a few microseconds off..

Wow, this one's waaaay better than my solution! And it's quite elegantly made.XD

Thanks for this one!:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.