Home > Backend Development > PHP Tutorial > php date function

php date function

WBOY
Release: 2016-07-30 13:30:13
Original
2774 people have browsed it

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)
Copy after login
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:

  • d - day of the month (01-31)
  • m - current month as a number (01-12)
  • Y - current year (Four digits)

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");
?>
Copy after login

The output of the above code is similar to this:

2006/07/11
2006.07.11
2006-07-11
Copy after login

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() function

to 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)
Copy after login

If we need to get the timestamp of a certain day, we only need to set the day parameter

of the

mktime() function:

<?php
$tomorrow = mktime(0,0,0,date("m")<span>,date("d")&#43;1</span>,date("Y"));
echo "明天是 ".date("Y/m/d", $tomorrow);
?>
Copy after login
The output of the above code is similar to this:

明天是 2006/07/12
Copy after login

Copyright 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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template