Home >Backend Development >PHP Tutorial >php date function
PHP’s date() function is used to format time or date.
PHP Date() Function
PHP Date() function formats timestamps into more readable dates and times.
Syntax
date(format,timestamp)
Parameters | Description |
---|---|
format | Required. Specifies the format of the timestamp. |
timestamp | Optional. Specify timestamp. The default is the current date and time. |
PHP Date - What is Timestamp?
The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also called Unix Timestamp.
PHP Date - Format Date
date() The first parameter of the function specifies how to format the date/time. It uses letters to represent date and time formats. Here is a list of some available letters:
You can find all the letters that can be used in the format parameter in our PHP Date reference manual.
You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formatting:
<?php echo date("Y/m/d"); echo "<br />"; echo date("Y.m.d"); echo "<br />"; echo date("Y-m-d"); ?>
The output of the above code is similar to this:
2006/07/11 2006.07.11 2006-07-11
PHP Date - Adding a Timestamp The second parameter of the date() function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.
In our example, we will use the
mktime() functionto create a timestamp for tomorrow. The
mktime() function returns a Unix timestamp for a specified date.Syntax
mktime(hour,minute,second,month,<span>day</span>,year,is_dst)
If we need to get the timestamp of a certain day, we only need to set the day parameter
of themktime() function: <?php
$tomorrow = mktime(0,0,0,date("m")<span>,date("d")+1</span>,date("Y"));
echo "明天是 ".date("Y/m/d", $tomorrow);
?>
The output of the above code is similar to this:
明天是 2006/07/12Copyright statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.
The above introduces the date function of PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.