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

怪我咯
Release: 2023-03-07 14:10:02
Original
9501 people have browsed it

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

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 http://www.php.cn/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 constantsDescription
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

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码


echo "Now:".date("Y-m-d")."<br/>";                   //输出当前日期

?>
Copy after login

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

<?php

echo "DATE_ATOM=".date(DATE_ATOM)."<br/>";

echo "DATE_COOKIE=".date(DATE_COOKIE)."<br/>";

echo "DATE_ISO8601=".date(DATE_ISO8601)."<br/>";

echo "DATE_RFC822=".date(DATE_RFC822)."<br/>";

echo "DATE_RFC850=".date(DATE_RFC850)."<br/>";

echo "DATE_RSS=".date(DATE_RSS)."<br/>";

echo "DATE_W3C=".date(DATE_W3C );
?>
Copy after login
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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!