Updated March 10, 2023
Introduction to TimeStamp in the PHP
The timestamp is generally a value with seconds, and its format is the same as the Greenwich Meantime. But the only difference here in the timestamp compared with Greenwich Meantime is that its first term will be Month, Date, and year, and the second term will be time with hours, minutes, and seconds. In this topic, we are going to learn about PHP Timestamp.
Syntax:
The syntax for the timestamp is
time()
Working with Timestamp in PHP
The Time() function in PHP is used to display the current date and the time. In the Timestamp function, we will not include any argument. The output of the time function is generally an integer. It contains all the values, including the second’s value also.
Example:
<?php
$a=time();
print($a);
?>
Output:
Explanation:
If we want today’s time and date, we use the function time(). First, it is assigned to a variable a. Then to display it. We use Print. It produces an output that is not understandable by us. The output is known as the timestamp. We convert the obtained output into an understandable type using PHP tools to understand the output clearly.
We generally use the getdata() function to convert the timestamp into the date. If we give the timestamp, it simply returns the output as time.
Keywords of PHP Timestamp
Some of the keywords which are used during the timestamp creation are:
a: It is used to write the time in am or pm
Example: 12 AM
A: It is used to write the time in AM or PM in capital words.
Example: 1 PM
D: It is used to write the day in a month.
Example:15
d: It is used to display a day in a week.
Example: wed
F: It is used to display the month’s name
Example: January
S: It is used to display the seconds
Example:20
Y: It is used to display the year
Example:2020
i: It is used to display the minutes.
Example:20 min
G: it is used to display the hour. Its format is 24 hours.
Example:14 hour
Examples of PHP Timestamp
Different examples are mentioned below:
Example #1
<?php
$a=time();
echo ($a. "\n");
echo (date ("Y-M-d", $a));
?>
Output:
Explanation:
Here we want the output of today’s date, year, and month. So first, we assigned a time function to the variable. Here we use the date function to get the time in the date format. As explained above, we want the year, month, and date, so we use the keywords Y, D, and M, and at the end, we will get the output as
2020-Feb-25
It means today’s date is 25, the year is 2020, and the month is February.
Example #2
<?php
$A =date ("y/m/D G:i:s", time());
print($A)
?>
Output:
Explanation:
Here we want the output in the month, day, year, hour, minute, and seconds .so we use the keywords m, D, y, G, I, and s. First, we assign the date format to a variable named A. then, we use the Print to display the output. The output displayed will be in the format of 20/02/Tue 6:52:28; it means today’s year is 2020, the month is February, and the day is Tuesday. Immediately it assigns the time and displays it with the present hour, minutes, and seconds.
Example #3
<?php
$A =date ("y/m/j G:i:s", time());
print($A)
?>
Output:
Explanation:
Here we want the output that contains the year, month, and day of the month and time with hours, minutes, and seconds. First, we assign the time function to a variable A. Then, to get the output, we use the Print. The output which is obtained will be as follows
20/02/25 6:55:23
It means today’s year is 2020, February, and the day of the month is 25. The time obtained contains the 6 hours 55 minutes and the 23 seconds as the output. The output contains the combination of the date and the time.
Things to Remember
- PHP is easy to install.
- PHP is dynamic.
- The timestamp is used to print the time.
- The code is written in the script format, and the output is displayed in the web browser.
- Easy to understand.
- There are 8 data types.
- There are five different types of operators.
- The timestamp is written with the function time()
- To convert it into understandable output, we use the function get()
- The timestamp is easy to learn and understand.
Recommended Articles
This is a guide to PHP Timestamp. Here we discuss the Working of PHP Timestamp, examples, and outputs. You may also have a look at the following articles to learn more –