PHP time() Function

What is PHP time() function?

PHP time() function provides current time as a number of second. Example 1703819446. The function calculates seconds from the 1st January 1970 00:00:00 GMT (which is known as UNIX Epoch) to the current time. This duration is known as timestamp of the current time. You can convert this seconds to human readable date and time format later.

Your current time depends on your timezone. Like two different timezones provide two different values of the current time, the time() function also provides two different values for the same current time depending on the timezone.

How PHP time() Function Works?

At first, the time function figuring out the default timezone of the server. For this it follows the steps serially-

  • At first it looks for any date_default_timezone_set() function set in the current script.
  • If not found, it looks for the date.timezone option in the php.ini file.
  • If not found, it takes UTC as the default timezone.

Then, it counts seconds between UNIX epoch and the default timezone’s time.

How to make PHP time() function readable?

What’s the use of the number of seconds that time() function returns if we can’t read date/time from it. To make the seconds human readable format (like day, month, year, hour, minutes, seconds), we use PHP date() function.

So, from a timestamp (which is just a number of some seconds) we human can’t read a date. We need to convert this seconds to a date and time format. And PHP date() function helps us to accomplish this.

Syntax:

time()

Parameters:

The function accepts no parameter.

Return Values:

The formatted date of the timestamp –

  • specified in the second parameter, or
  • of the local time if the second parameter is not specified.

Examples:

Example 1: Retrieving timestamps (seconds) of the current time-

<?php
echo time();
?>

Output:

1703824629

Explanation:

The time() function returns the current server’s time in seconds.

Example 2: Finding current date and times of multiple timezones-

<?php
echo "Current timezone is: " . date_default_timezone_get();
echo "<br />";
$currentSeconds = time();
echo "And, current time in this timezone is: " . date("m/j/Y g:i:s A", $currentSeconds);
echo "<br />";
date_default_timezone_set("Asia/Tokyo");
echo "New timezone is: " . date_default_timezone_get();
echo "<br />";
$currentSeconds = time();
echo "And, current time in this timezone is: " . date("m/j/Y g:i:s A", $currentSeconds);
?>

Output:

Current timezone is: America/New_York
And, current time in this timezone is: 12/28/2023 11:37:09 PM
New timezone is: Asia/Tokyo
And, current time in this timezone is: 12/29/2023 1:37:09 PM

Explanation:

As you see, the current timezone of the server is America/New_York and the time() function returns the seconds according to this timezone in line 4. Then, we use date() function to convert the seconds into human readable format.

We then change the timezone to Asia/Tokyo in line 7 and line 11 shows current time of the Asia/Tokyo timezone.

Note, when you run the script you’ll see different time as the clock is always ticking.

Example 3: Finding date and time after 7 days from now-

<?php
$currentSeconds = time();
echo "Current time in this timezone is: " . date("m/j/Y g:i:s A", $currentSeconds);
$secondAfter7days = time() + 7 * 24 * 60 * 60;
echo "<br />";
echo "Time after 7 days is: " . date("n/j/Y g:i:s A", $secondAfter7days);
?>

Output:

Current time in this timezone is: 12/29/2023 1:37:09 PM
Time after 7 days is: 1/5/2024 1:37:09 PM

Explanation:

In line 4, time() produces current time in second. With this, we then add seconds equivalent of 7 days by 7*24*60*60.

Practical Usages of time() Function:

PHP time() function is widely used to get the current time or to get the time of a specific timezone.

Notes on time() Function:

To get the correct value of the time() function make sure that your server time and your local time is same. If your server is in different timezone you’ll get wrong value of time() function.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP time() Function

When you’ll work with date and time in your PHP application, you’ll use time() function now and then. It is one of the frequently used date and time functions. You can do many date and time operations when you get the timestamp from this function.

Reference:

https://www.php.net/manual/en/function.time.php