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:

$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

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.