Home >Backend Development >PHP Problem >How to display date and time using PHP

How to display date and time using PHP

PHPz
PHPzOriginal
2023-04-26 10:29:312405browse

PHP is a very popular server-side programming language that can be used to handle the background logic of a website and dynamically generate web content. In many cases, we need to display the current date or date and time in a web page. In this case, we need to use PHP to obtain the system time and format it to meet our requirements.

This article will introduce how to use PHP to display date and time, and provide some common date formatting functions and sample code.

Get the current date and time

In PHP, we can use the date() function to get the current date and time. The syntax of this function is as follows:

string date ( string $format [, int $timestamp = time() ] )

where , the $format parameter specifies the format of the date and time, which can be a string containing various formatting characters, such as "Y-m-d H:i:s" indicating year-month-day hour: minute: second. The "timestamp" parameter is optional. If not specified, it defaults to the current time.

For example, the following code will get the current date and time and output it in the format of "Y-m-d H:i:s":

<?php
echo date("Y-m-d H:i:s");
?>

The output result is similar to: "2022-05-25 13:47:30".

Common date formatting characters

The following table lists common date formatting characters and their meanings:

## HHour number in 24-hour format (for example: 13)hHour number in 12-hour format (for example: 1 or 01)iNumber of minutes (for example: 47)sNumber of seconds (for example: 30) AUppercase morning or afternoon (for example: PM)aLowercase morning or afternoon Afternoon (for example: pm)
Formatting Character Description
Y Four-digit year (for example: 2022)
y Year with two digits (for example: 22)
m Month with two digits (for example: 05)
n Month (without leading zero, for example: 5)
d The day of the month ( For example: 25)
j The day of the month (without leading zeros, for example: 25)
Use date formatting characters to display date and time

Now, we can use the above date formatting characters to customize The date and time format you need. The following are some common date formatting methods and their sample codes:

    Display the current time in the format: "Y-m-d H:i:s":
<?php echo date("Y-m-d H:i:s"); ?>
    Display the current date in the format: "Y-m-d":
<?php echo date("Y-m-d"); ?>
    Display the current time in the format: "h:i:s A" (12-hour format, with afternoon mark ):
<?php echo date("h:i:s A"); ?>
    Display the current time, the format is: "H:i:s" (24-hour format):
<?php echo date("H:i:s"); ?>
    Display the current time Date and time, the format is: "d-m-Y H:i:s":
<?php echo date("d-m-Y H:i:s"); ?>
    Display the current year, the format is: "Y":
<?php echo date("Y"); ?>
    Display the current month, the format is: "m":
<?php echo date("m"); ?>
    Display the English abbreviation of the current month, the format is: "M":
<?php echo date("M"); ?>
    Display the day of the week of the current date, the format is: "l":
<?php echo date("l"); ?>
    Display the English abbreviation of the current date, the format is: "D":
<?php echo date("D"); ?>
    Displays the current day of the year in the format: "z":
<?php echo date("z"); ?>
    Displays the current timestamp in the format: "U ”:
<?php echo date("U"); ?>
In addition to the above examples, you can also combine different date formatting characters according to your own needs to get a more personalized date and time format.

Use localized date and time formats

In some cases, we need to use localized date and time formats according to different locales. At this time, we can use the strftime() function, which can display the date and time according to the language and configuration of the local environment. The following is an example:

<?php
setlocale(LC_TIME, "zh_CN.utf8"); // 设置为中文环境
echo strftime("%Y年%m月%d日 %H:%M:%S"); // 输出类似“2022年05月25日 13:47:30”
?>
It should be noted that in order to correctly support localized date and time formats, the relevant locale support needs to be installed and enabled. This is achieved by installing the locale package on the Linux server.

Conclusion

In web development, displaying date and time is a very common requirement. In PHP, you can use the date() function to get the current date and time and format it according to your needs. This article introduces some common date formatting characters and sample codes. I hope it can be helpful to everyone.

The above is the detailed content of How to display date and time using PHP. 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