PHP - Unix Timestamp Functions

PHP - Unix Timestamp Functions

Colleagues hello to all.

In today's article, we will talk about how to get the system Unix Timestamp in the PHP programming language.

Unix Timestamp Timestamp - is an example of time tracking in seconds. The count starts in the Unix epoch on January 1, 1970 UTC. So  unix time timestamp is the number of seconds between a certain date and Unix epoch. Note that at this point in time, technically   unix time timestamp does not change no matter where you are on the globe . Unix Timestamp is useful to keep track of and sort dated information in dynamic and distributed applications, both interactively and client-side.

In the PHP programming language, there are several ways to get this same Unix Timestamp, and today we will look at these methods.

 

time()

In the first variant, we will consider a function called time(). This function does not take any parameters, but simply returns the current Unix timestamp in seconds.

A simple usage example.

php> time();

unix_timestamp_php

As you can see from the example, the time() function returned us a set of numbers, this is the Unix Timestamp.


 

microtime()

Instead of the time() function, you can use the  microtime() function. The  microtime() function is very similar to the time() function, only it has a small difference. The microtime() function returns the Unix timestamp in microseconds.

A simple usage example.

php> microtime();

unix_timestamp_php


 

DateTimeImmutable

If you like to use the object-oriented style, then you can use a php class called DateTimeImmutable for this case. We need to create an object of this class and then call a method on this object called getTimestamp.

php> $date = new DateTimeImmutable();
php> echo $date->getTimestamp();

unix_timestamp_php


 

DateTime

In addition to the DateTimeImmutable class, you can use the DateTime class. We also need to create an object of this class, and then call a method called getTimestamp on this object.

php> $date = new DateTime();
php> echo $date->getTimestamp();

unix_timestamp_php


 

Thank you all, I hope that my article helped you in some way.

SIMILAR ARTICLES

PHP - How to remove spaces from the beginning and end of a string
PHP - How to remove spaces from the beginning and end of a string
WEB
Read more
PHP - date formatting. date() function.
PHP - date formatting. date() function.
WEB
Read more
PHP - How to determine timezone - timezone
PHP - How to determine timezone - timezone
WEB
Read more