0

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

Edited by jrhitokiri: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by jrhitokiri
0
$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.

Edited by Shanti C: n/a

0

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

0

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..

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.