John Davidson

Why is unix timestamp appearing differently on local machine versus production server PHP: round(microtime(true) * 1000)

0 comments
Message:


I'm using a timestamp in a function and generating it in my local Wordpress Docker site like this (PHP):


round(microtime(true) * 1000)


Works as expected and gives me something like 1659069789179.


However, the same code gives me something like 1.6590697862E+12 on my production Wordpress server.


Anybody have insight into why this happens and how I can get the current timestamp consistently across environments?



My Answer:
The difference in the output you are seeing is due to the way PHP handles floating point numbers and how it converts them to strings for display.

When you run `round(microtime(true) * 1000)` on your local machine, PHP is able to convert the result to a string representation of the timestamp without any issues. However, on your production server, PHP is converting the result to scientific notation (1.6590697862E 12) because the number is too large to be displayed in standard decimal notation.

To get a consistent timestamp across environments, you can use the `number_format()` function to format the timestamp as a string with a specific number of decimal places. For example:

php
$timestamp = round(microtime(true) * 1000);
$timestampString = number_format($timestamp, 0, '.', '');


This will give you a timestamp in standard decimal notation without scientific notation, regardless of the size of the number.

Rate this post

3 of 5 based on 6966 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us