Home >Backend Development >PHP Tutorial >Detailed explanation of date() function in PHP

Detailed explanation of date() function in PHP

autoload
autoloadOriginal
2021-03-22 16:55:104387browse

Syntax:

string date ( string $format [, int $timestamp ] )
  • format Specifies the format of the timestamp.

  • timestamp specifies the timestamp. The default is the current date and time.

Among them, the most commonly used characters in format are:

  • d - represents the day of the month (01 - 31)

  • m - represents the month (01 - 12)

  • Y - represents the year (four digits)

Example:

<?php
    echo date("Y/m/d") . "<br>";// 2021/3/22
    echo date("Y.m.d") . "<br>";// 2021.3.22
    echo date("Y-m-d");          // 2021-3-22
?>

Use time() to obtain the timestamp

                                                                                  ()In PHP, you can get a number, that is, a timestamp. This number represents the time from Greenwich Mean Time (1970-01-01) to the present.

1. So how to change this timestamp number into date format? You must use the date() function

$datetime=time(); //获取时间戳
echo date("Y-m-d H:i:s",$datetime);//将时间戳转换为要求的日期时间格式
echo date("Y年m月d日 H点i分s秒",$datetime);//其实上面的横杠(-)可以换成任意字符

2. How to convert the date into timestamp format, that is, into a string of numbers (seconds)

$str=&#39;2015-12-18 12:23:35&#39;;//设置时间变量
echo strtotime($str);//将标准的日期时间格式转为以秒为单位的时间戳数字,这里要注意,用strtotime()函数,要求被转换的时期时间为标准格式,如果用了年、月、日这种就不行了,要先转换为标准格式才行!

Recommended:

php video tutorial php tutorial

The above is the detailed content of Detailed explanation of date() function 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