0

I have written this Javascript to convert a timestamp to something readable. It works perfectly. However I need to do it with PHP but don't know how. Obviously I don't want an alert of the time, but I'd like to have it as a PHP variable. Any ideas?

<script>
var bmsTime ="39845.03";
var date = new Date('31 dec 1899');
date.setTime(date.getTime() + bmsTime* 24 * 60 * 60 *1000);

alert (date);
</script>
1
  • What is the format of the JavaScript date output? Commented Aug 13, 2011 at 18:04

2 Answers 2

1

Use the date function, it takes an additional parameter called timestamp. But in php timestamp is number of seconds, not milliseconds as in javascript, so divide it by 1 000:

echo date('l jS \of F Y h:i:s A', $javascript_timestamp / 1000);
Sign up to request clarification or add additional context in comments.

Comments

0

The short answer: You cannot properly do this, as you do not know the timezone the browser/client is using.

Long answer (using the server timezone - or whatever is configured for PHP):

$bmsTime = 39845.03;
$date = mktime(0, 0, 0, 12, 31, 1899);
$date += $bmsTime * 24 * 60 * 60; // $date are the seconds relative to "the epoc" (1970-01-01 UTC)

echo date('c', $date);

3 Comments

u're wrong. U don't need to multiply by 1000, because PHP timestamp is in seconds, not milliseconds as JS
@Shi it seems mktime won't allow me go before the epoch.
@Peter What does it do? var_dump(mktime(0, 0, 0, 12, 31, 1899)); returns int(-2209075200) here, so it works for me in PHP 5.3.5. Maybe: Before PHP 5.1.0, negative timestamps were not supported under any known version of Windows and some other systems as well. Therefore the range of valid years was limited to 1970 through 2038. -- php.net/manual/en/function.mktime.php

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.