Recreating PHP’s time() function in JavaScript

Recreating PHP's time() function in JavaScript
Sharing is Caring

PHP’s time() function returns the current unix timestamp. JavaScript doesn’t exactly have the same functionality, but it’s pretty close. Put a different and more understandable way, it returns the current time measured in seconds since the Unix Epoch (00:00:00 UTC on 1 January 1970).

The time function in PHP doesn’t accept any parameters because it literally just returns the current time measured in seconds since the Unix epoch.

Using the time function in PHP is really easy, it can be used like so:

<?php
$timeInSeconds = time();
?>

JavaScript has a similar way of getting the time but it’s in milliseconds since the Unix Epoch.There are 1000 milliseconds in a second, so getting the current time in seconds can be done pretty easily.

Math.floor(new Date().getTime() / 1000)
Sharing is Caring