Syntax:
The syntax of the time() function is given below.
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.
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“.
//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.
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.
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.
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.