A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in PHP.
To run the program, save it as epoch.php and use the PHP interpreter:
In PHP, we use the time() function to get the current Unix timestamp in seconds. For milliseconds and microseconds, we multiply the timestamp by 1000 and 1000000 respectively.
To convert a Unix timestamp back to a readable date and time, we can use the DateTime class or the date() function.
PHP doesn’t have a built-in function for nanoseconds precision, so we’ve used microseconds (millionths of a second) as the highest precision available.
Next, we’ll look at another time-related task: time parsing and formatting in PHP.