Search code examples
phpcoldfusioncoldfusion-8

Comparing Coldfusion and PHP timestamp in milliseconds


I'm creating a timestamp in CF8 using a custom function. It's then being passed via query string to PHP and compared, again, to the current timestamp (using time()). Since CF8 doesn't have a built-in millisecond option for DateDiff(), I appended 3 zeroes to the end of the returned value (which is in seconds).

But, when I compare it against the PHP timestamp, the difference should be in seconds, but I'm coming up with a multi-hour difference.

<cfscript>
function GetEpochTime() {
    datetime = Now();
    return DateDiff("s", "January 1 1970 00:00", datetime) & "000"; //zeroes padded for milliseconds
}
</cfscript>
<cfset foo = getepochtime() /> //Returns time stamp

For the sake of argument, the CF timestamp reads 1323344375000. When compared against PHP's stamp with a value of 1323362710000, the difference is 18,555,000 milliseconds (over 300 minutes). Real time that has passed is maybe 2 seconds.

$php_ts = time() * 1000; //PHP timestamp is in seconds, too
$cf_ts = $_GET['coldfusion_timestamp'];
echo $ts_difference = ($php_ts - $cf_ts) / (1000 * 60); //Difference in minutes

Where is my conversion failing?


Solution

  • You're not taking the server's local time into consideration, within the context of the GMT offset.

    Modify your getEpochTime() to:

     return DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), datetime) & "000";
    

    Source: GetEpochTimeFromLocal() (Rob-Brooks Bilson)