Home >Backend Development >PHP Problem >How to display date and time using PHP
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:
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) |
Hour number in 24-hour format (for example: 13) | |
Hour number in 12-hour format (for example: 1 or 01) | |
Number of minutes (for example: 47) | |
Number of seconds (for example: 30) | |
Uppercase morning or afternoon (for example: PM) | |
Lowercase morning or afternoon Afternoon (for example: pm) |
<?php echo date("Y-m-d H:i:s"); ?>
<?php echo date("Y-m-d"); ?>
<?php echo date("h:i:s A"); ?>
<?php echo date("H:i:s"); ?>
<?php echo date("d-m-Y H:i:s"); ?>
<?php echo date("Y"); ?>
<?php echo date("m"); ?>
<?php echo date("M"); ?>
<?php echo date("l"); ?>
<?php echo date("D"); ?>
<?php echo date("z"); ?>
<?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 formatsIn 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. ConclusionIn 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!