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

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

王林
Release: 2023-03-10 13:58:01
Original
4791 people have browsed it

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);
Copy after login

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;
?>
Copy after login

The output results are as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template