php

PHP time() Function

The current date and time are calculated based on the UNIX timestamp value in Linux operating system. The timestamp value is calculated based on the time passed in seconds from 1st January 1971. PHP has many built-in functions to do date and time operations using PHP. The time() function calculates time-based on the timestamp value. The use of the time() function has been explained in this tutorial using multiple examples.

Syntax:

The syntax of the time() function is given below.

int time()

The function does not argue, and it returns an integer value as a current time in seconds based on the timestamp.

Time Formatting Characters

Many types of characters are used to format the time value. Some commonly used formatting characters are given below.

Character Purpose
h It is used to display the hour in a 12-hour format with the leading zero. (01 to 12)
H It is used to display the hour in 24-hour format with the leading zero. (00 to 23)
i It is used to display the minute with the leading zero. (00 to 59)
s It is used to display the second with the leading zero. (00 to 59)
a It is used to display the lowercase Ante meridiem and Post meridiem (am or pm)
A It is used to display the uppercase Ante meridiem and Post meridiem (AM or PM)

time() Function Examples

The uses of the time() function have been shown in the next part of this tutorial by using multiple examples.

Example-1: Print the Current Time

Create a PHP file with the following script that will print the timestamp value and the formatted time of the current time. Here, the timestamp value returned by the time() function has been used in the date() function to get the human-readable time value.

<?php

//Read the current timestamp value

$tm = time();

//Print the timestamp value of the current time

echo "The timestamp value of the current date is: <b>".$tm."</b>";

//Print the formatted current time

echo "<br/> The current time is: <b>".date('h:m:s A', $tm).'</b>';

?>

Output:

The following output will appear after executing the above script.

Example-2: Print the Current Time Based on the Timezone

Create a PHP file with the following script that will print the formatted time of the current time based on the time zone value. The first time() function has been used in the script to read the timestamp of the current time without setting any time zone value. The second time() function has been used in the script to read the timestamp of the current time after setting the default time zone value to “Asia/Dhaka“.

<?php

//Read the current timestamp value based on default timezone

$tm = time();

//Print the current time based on default timezone

echo "The current time based on default timezone is: <b>".date('h:m:s A', $tm).'</b>';

//Set the default timezone

date_default_timezone_set("Asia/Dhaka");

//Read the current timestamp value based on Asia/Dhaka timezone

$tm = time();

//Print the current time based on Asia/Dhaka timezone

echo "<br/> The current time based on Asia/Dhaka timezone is: <b>".date('h:m:s A', $tm).'</b>';

?>

Output:

The following output will appear after executing the above script.

Example-3: Print the Current Date

The current date can also be retrieved from the timestamp value returned by the time() function. Create a PHP file with the following script that will print the formatted date by using the returned value of the time() function in the second argument of the date() function. The format string used in the date() function will print the day of the month with an ordinal number, the month name in short form, and the four-digit year value.

<?php

//Read the current timestamp value

$tm = time();

//Print the timestamp value of the current time

echo "The timestamp value of the current date is: <b>".$tm."</b>";

//Print the formatted current date

echo "<br/> Today is: <b>".date('jS F, Y', $tm).'</b>';

?>

Output:

The following output will appear after executing the above script.

Example-4: Print the Previous Time

The previous time can be retrieved by deducting the value from the timestamp value. Create a PHP file with the following script to print the previous time by deducting 2 hours from the current timestamp value. Two hours equal to 120*60 seconds have been used in the script to read the time value two hours back. The deducted value has been used in the date() function to print the formatted time. Both the current and previous times will be printed after executing the script.

<?php

$tm1 = time();

//Print the current time

echo "The current time is: <b>".date('h:m:s A', $tm1).'</b>';

$tm2 = time()-120*60;

//Print the previous time

echo "<br/> The previous time is: <b>".date('h:m:s A', $tm2).'</b>';

?>

Output:

The following output will appear after executing the above script.

Example-5: Print the Next Date

The future date and time can be calculated by adding the timestamp value. Create a PHP file with the following script to print the next date by adding 24 hours in seconds with the current timestamp value. 24 hours equal to 24*60*60 seconds that have been used in the script to read the timestamp value of the next day. The added value has been used in the date() function to print the formatted date of the next day. Both the current date and the next day’s date will be printed after executing the script.

<?php

//Print the current date

echo 'Today is '. date('l, jS F, Y');

//Read the timestamp value of the next day

$NextDay = time() + 24 * 60 * 60;

//Print the date of the next day

echo '<br/>Tomorrow is '. date('l, jS F, Y', $NextDay);

?>

Output:

The following output will appear after executing the above script.

Conclusion

Different ways of using the time() function to read date and time have been shown in the examples of this tutorial to help the PHP users to apply this function properly in their script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.