Home  >  Article  >  Backend Development  >  How to convert timestamp to year, month and day in php

How to convert timestamp to year, month and day in php

王林
王林Original
2021-06-10 15:00:594754browse

The way PHP converts the timestamp into year, month and day is to use the date() function to format the timestamp and return the formatted date string, such as [date("Y-m-d H:i: s")].

How to convert timestamp to year, month and day in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

In fact, if we want to convert the timestamp into year, month and day, it is very simple, because PHP provides us with a ready-made function, which is the date() function.

date() function is used to format local date and time and return the formatted date string.

Syntax:

date(format,timestamp);

Returns the string generated by converting the integer timestamp according to the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time().

Specific example:

<?php
// 设置时区
date_default_timezone_set("PRC"); 
// 打印当前时间  PHP_EOL 换行符,兼容不同系统
echo date("Y-m-d H:i:s")  . PHP_EOL;
echo date("Y 年 m 月 d 日 H 点 i 分 s 秒")  . PHP_EOL;
// 指定时间
$time = strtotime("2018-01-18 08:08:08");  
// 将指定日期转成时间戳 
echo date("Y-m-d H:i:s", $time)  . PHP_EOL;
?>

The output results are as follows:

2018-01-31 22:09:35
2018 年 01 月 31 日 22 点 09 分 35 秒
2018-01-18 08:08:08

Related video sharing: php video tutorial

The above is the detailed content of How to convert timestamp to year, month and day 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