In the daily use of PHP, we inevitably need to use time. PHP has a built-in date() function to help us unlock the way of time. This article will take you through it. Take a look at the code of time.
First, let’s take a look at the syntax of the date()
function
date ( string $format , int $timestamp = ? )
$timestamp: optional. Specifies an integer Unix
timestamp. The default is the current local time (time()
).
$format: Output date string
Format
Among them, the more commonly used:
m -- --- with Month with leading 0 digits from 01 to 12
Y -- --- Year with 4 digits Example: 1999 or 2003
j ----- The day of the month, without leading 0 From 1 to 31
<?php $today = date("F j, Y, g:i a"); //April 16, 2021, 2:55 am echo $today."<br>"; $today = date("m.d.y"); //04.16.21 echo $today."<br>"; $today = date("j, n, Y");//16, 4, 2021 echo $today."<br>"; $today = date("Ymd");//20210416 echo $today."<br>"; $today = date('h-i-s, j-m-y, it is w Day z ');//02-55-37, 16-04-21, 5530 5537 5 Friam21 105 echo $today."<br>"; $today = date('\i\t \i\s \t\h\e jS \d\a\y.'); //it is the 16th day. echo $today."<br>"; $today = date("D M j G:i:s T Y");//Fri Apr 16 2:55:37 UTC 2021 echo $today."<br>"; $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); //02:04:37 m is month echo $today."<br>"; $today = date("H:i:s");//02:55:37 echo $today."<br>"; $today = date("Y-m-d H:i:s");//2021-04-16 02:55:37 echo $today."<br>"; ?>
<?php $datetime=time(); //获取时间戳 echo $datetime."<br>"; echo date("Y-m-d H:i:s",$datetime);//将时间戳转换为要求的日期时间格式 echo date("Y年m月d日 H点i分s秒",$datetime); ?>
输出: 03:05:33 2021-04-16 03:05:33 1618542333 2021-04-16 03:05:332021年04月16日 03点05分33秒
Recommendation: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of Password date() function for time in PHP. For more information, please follow other related articles on the PHP Chinese website!