Jump to content
  • Advertisement
Sign in to follow this  
kooktroop

[web] Timestamp string to timestamp (PHP)

This topic is 5448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to convert a string passed to the script via GET into a timestamp of int form to be used with date() and the associated functions. page.php?time=1097749860000
function strtoint( $str ) {
	$ret = 0;
	$mult = 1;
	for ( $i = strlen( $str ) - 1; $i >= 0; $i-- ) {
	print $str{$i} . ' * ' . $mult . ' = ' . ( $str{$i} * $mult ) . '<br />';
		$ret += $str{ $i } * $mult;
		$mult *= 10;
	}
	return $ret;
}

$time = $_GET[ 'time' ]; // doesnt work
$time = intval( $_GET[ 'time' ] ); // doesnt work
$time = strtoint( $_GET[ 'time' ] ); // returns a float because the number is larger than an int, so doesnt work.

I've tried everything (i think) an answer or at least a point in the right direction would be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
The 'time-string' you pass seems to be wrong.
Try page.php?time=1041526713

<?
$time = $_GET[ 'time' ]; // works fine
if(is_numeric($time)) echo "numeric<br />";
echo $time;
echo "<br />";
echo date("H:i:s", $time); // works fine
echo "<br />";
?>

PHP (usually) doesn't care if it's an int or a string.

Share this post


Link to post
Share on other sites
You shouldn't need to convert the GET variable to an integer, PHP doesn't care about the variables type, except if you have explicitly set a type.

This means that you can directly parse it to a function after evaluating it as being a number, preferably with is_numeric().

if (is_numeric($_GET['time']))
echo strftime('%d.%m.%Y', $_GET['time']);
else
echo 'Invalid Timestamp!';


Furthermore I'd advise you to use strftime() instead of date() as you can see in my example. The reason is simple: strftime() respects the LOCALE you have set so that month and weekday names for example are directly translated.

More info on this:
- http://de2.php.net/manual/en/function.strftime.php
- http://de2.php.net/manual/en/function.setlocale.php
- http://de2.php.net/manual/en/function.is-numeric.php

Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!