Home  >  Article  >  Backend Development  >  Detailed explanation of PHP date() function to obtain the current date and time instance

Detailed explanation of PHP date() function to obtain the current date and time instance

怪我咯
怪我咯Original
2017-04-18 17:57:149383browse

When we explain how to get the current timestamp instance, we use the date() function to get the current time and date. In this chapter, we will introduce it to you. The "date" function that gets the current date and time.

Usage of date function

In PHP, use the date() function to obtain the current time and date. Its syntax format is as follows

date(format,timestamp);

date The () function will return a string generated by the timestamp parameter according to the specified format. The timestamp parameter is optional. If not written, the current time will be used. The format parameter allows developers to output dates in the format they specify. Regarding format parameters, we have written a special article. You can check it here //m.sbmmt.com/php-weizijiaocheng-360500.html.

Here are several predefined constants for time and date, as shown in the following table. These constants provide standard date expression methods and can be used for date format functions.

Predefined constants about time and date

##DATE_ISO8601 ISO-8601 formatDATE_RFC822 RFC822 formatDATE_RFC850 RFC850 formatDATE_RSS WORSS formatDATE_W3C World Wide Web Consortium format
Predefined constants Description
DATE_ATOM Atomic Clock Format
DATE_COOKIE HTTP Cookies Format
We will have examples below to show you the differences between various constants

date function example one

This example Let me demonstrate the usage of the date() function. The code is as follows

";                   //输出当前日期

?>

The code running result:

Detailed explanation of PHP date() function to obtain the current date and time instance


Date function example two

This example will compare the output of each constant. The sample code is as follows

";

echo "DATE_COOKIE=".date(DATE_COOKIE)."
"; echo "DATE_ISO8601=".date(DATE_ISO8601)."
"; echo "DATE_RFC822=".date(DATE_RFC822)."
"; echo "DATE_RFC850=".date(DATE_RFC850)."
"; echo "DATE_RSS=".date(DATE_RSS)."
"; echo "DATE_W3C=".date(DATE_W3C ); ?>

The code running result:

Detailed explanation of PHP date() function to obtain the current date and time instance

The above is the output of predefined constants. In the next section, we will explain how to obtain date information with PHP

The above is the detailed content of Detailed explanation of PHP date() function to obtain the current date and time instance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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