5 examples to learn PHP Time function in 15 minutes

The Time function of PHP

The time PHP function returns the total umber of seconds since the Unix Epoch (January 1, 1970, 00:00:00 GMT).

Experience this example online



As you can see, the output is a long number that generally is not understandable by the end user in terms of presenting the time to your web pages.You have to calculate this in order to understand the output.

To make it meaningful, you have to use it with date() function in order to format it and making it understandable as shown below.

The time function example with date

The following example will display the date on the basis of returned value of PHP time function.

PHP time function

Experience this example online



In the above example, we used the time function and assigned to $t variable that is used as a parameter of the date function. Then it displayed the Year, month and day on that basis.

Time in hour: minute: second format example

Following example shows using the time function with the date to display the time.

PHP-time

Experience this example online



The output will be according to your system time where the script is running.

Time and date display example

The following example displays the date and time by using the time and date function in m/d/y  h: i: s format.

Experience this example online



The output will be current date and time in the given format.

For more on time formatting also see date() function and PHP time parameters.

 

Also read: PHP mktime | timezone usage | getdate() function


Was this article helpful?

Related Articles

Leave A Comment?