Home  >  Article  >  Backend Development  >  Password date() function for time in PHP

Password date() function for time in PHP

autoload
autoloadOriginal
2021-04-16 11:14:442622browse

Password date() function for time in PHP

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

  • ## c ----- ISO 8601 date and time (new in PHP 5 ) 2004-02-12T15:19:21 00:00

  • W ---- The number of weeks in the year specified by ISO-8601, Monday is regarded as the beginning of the week. (Newly added in PHP 4.1.0) Example: 42 (42nd week of this year)

Get the current time

<?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(&#39;h-i-s, j-m-y, it is w Day z &#39;);//02-55-37, 16-04-21, 5530 5537 5 Friam21 105
echo $today."<br>";  
$today = date(&#39;\i\t \i\s \t\h\e jS \d\a\y.&#39;); //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(&#39;H:m:s \m \i\s\ \m\o\n\t\h&#39;); //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>";                   
?>

has a second parameter:

<?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秒

We can see that the second parameter is a timestamp. This number represents the time from Greenwich Mean Time (1970-01-01) to now.

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!

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