Home > Backend Development > PHP Tutorial > Introduction to PHP functions: date() function

Introduction to PHP functions: date() function

WBOY
Release: 2023-11-03 14:42:02
Original
1249 people have browsed it

Introduction to PHP functions: date() function

PHP function introduction: date() function

PHP is a programming language widely used in website development and has powerful functions for processing dates and times. In PHP, the date() function is a very commonly used function, which is used to return the current date and time, or any specified date and time.

The syntax of the date() function is as follows:
date(format,timestamp)

Among them, the format parameter is required and is used to specify the output format of date and time. The timestamp parameter is optional and is used to specify a timestamp. If timestamp is not provided, the current date and time will be used.

The following uses specific code examples to illustrate the use of the date() function.

<?php
// 1. 返回当前的日期和时间
echo date("Y-m-d H:i:s"); // 输出:2022-10-31 19:30:15
echo "<br>";

// 2. 返回当前的日期
echo date("Y-m-d"); // 输出:2022-10-31
echo "<br>";

// 3. 返回当前的时间
echo date("H:i:s"); // 输出:19:30:15
echo "<br>";

// 4. 返回年份
echo date("Y"); // 输出:2022
echo "<br>";

// 5. 返回月份
echo date("m"); // 输出:10
echo "<br>";

// 6. 返回日
echo date("d"); // 输出:31
echo "<br>";

// 7. 返回星期几
echo date("l"); // 输出:Monday
echo "<br>";

// 8. 使用时间戳来获取指定的日期和时间
$timestamp = mktime(10, 30, 0, 12, 25, 2022);
echo date("Y-m-d H:i:s", $timestamp); // 输出:2022-12-25 10:30:00
echo "<br>";

// 9. 将时间字符串转换为时间戳,并获取指定的日期和时间
$time_str = "2022-12-25 10:30:00";
$timestamp = strtotime($time_str);
echo date("Y-m-d H:i:s", $timestamp); // 输出:2022-12-25 10:30:00
echo "<br>";
?>
Copy after login

In the above code example, we use the date() function to output dates and times in various formats through different formatting parameters. Such as outputting the current date and time, year, month, day, day of the week, etc.

We can see that the format parameters of the date() function are very rich. For specific formatting parameters, please refer to the PHP official documentation, and I will not list them one by one here.

It should be noted that the return result of the date() function is a string, so we can directly output the result through echo.

In summary, PHP's date() function is a very practical function that can easily obtain and process dates and times. By specifying appropriate formatting parameters, we can obtain the required date and time information. It is widely used in website development and is very flexible and powerful.

The above is the detailed content of Introduction to PHP functions: date() function. For more information, please follow other related articles on the PHP Chinese website!

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