0

I'm trying to convert a timestamp string to Date in PHP. I have following code

date_default_timezone_set('Asia/Calcutta');
$time = (int)$_POST['timestamp'];
$timeString = date('H:i:s',$time);

Howevever, I always get timeString as 08:44:07 no matter what. I figured that timestamp is greater than what 32 bit integer could store, probably that's why this conversion always gives a default maximum value for integers.

How can I create a 64-bit integer in PHP? I have a 64-bit machine and 64-bit Windows 7 running on it. Is there a better way to convert timestamp string to local time?

EDIT: timestamp as a string is being sent by javascript - "1330582437883"

3
  • Please show some example values and the result you would expect. Commented Mar 1, 2012 at 6:02
  • Your figuring is wrong, you do not need a 64-bit UNIX timestamp, that would be some ridiculous amount of years into the future. What is the output of a var_dump( $_POST['timestamp']); Commented Mar 1, 2012 at 6:02
  • It turns out I was passing in timestamp as milliseconds, whereas PHP expects timestamp in seconds. I'll edit my question to provide the example values Commented Mar 1, 2012 at 6:17

1 Answer 1

6

My completely baseless stab into the dark:

The timestamp value is coming from Javascript. Javascript measures timestamps in milliseconds, not seconds like PHP. So the value is 1000 times greater than what PHP expects. Divide the value by 1000 before sending it to PHP. Alternatively do that server side, for which you'll probably have to use bcdiv to get a precise value.

Sign up to request clarification or add additional context in comments.

1 Comment

Your baseless stab was pretty spot on!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.