Home  >  Article  >  Backend Development  >  How to convert timestamp to date in php and make the month go to 0

How to convert timestamp to date in php and make the month go to 0

藏色散人
藏色散人Original
2021-06-30 11:15:011716browse

How to convert php timestamp to date and set the month to 0: first create a PHP sample file; then obtain the timestamp through the time function; finally pass "date("Y-n-j G:i:s"); "Method to convert the timestamp into a date and set the month to 0.

How to convert timestamp to date in php and make the month go to 0

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to convert timestamp to date in php and make the month go to 0?

php date format should not be zero. When formatting the timestamp, there are solutions with and without 0

Sometimes the date and time are output in the web page, and there is something before the month and date. A 0 always feels redundant. Today we will share some detailed parameters about whether to display 0 or not to display 0 in front of the month and date in PHP timestamp, as follows:

Example:

  • According to echo date("Y-m-d H:i:s);

the page output result is: 2016-05-22 09:14:08

  • According to echo date("Y-n-j G:i:s");

the page output result is: 2016-5-22 9:14:08

This is just a very common use of functions, see below

  • Getting the current time method date()

is very simple, This is the method to get time, the format is: date($format, $timestamp), format is the format, timestamp is the timestamp – fillable parameters.

  • Get timestamp method time( ), strtotime()

Both methods can obtain the unix timestamp in php, time() can be obtained directly, and strtotime($time, $now) can obtain the time The format is converted to a timestamp, and $time is required. If you understand this and want to know more, please continue reading.

  • date($format) usage

For example:

echo date(‘Y-m-d’) ,输出结果:2012-03-22
echo  date(‘Y-m-d H:i:s’),输出结果:2012-03-22 23:00:00
echo  date(‘Y-m-d’, time()),输出结果:2012-03-22 23:00:00(结果同上,只是多了一个时间戳参数)(时间戳转换为日期格式的方法)
echo  date(‘Y’).’年’.date(‘m’).’月’.date(‘d’).’日’,输出结果:2012年3月22日

These are just a few examples, just modifications of the format. The following is the meaning of each letter in the format:

Detailed explanation of various parameters:

a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"
D - 星期几,三个英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小时制的小时; 如: "01" 至 "12"
H - 24 小时制的小时; 如: "00" 至 "23"
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"
i - 分钟; 如: "00" 至 "59"
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"
M - 月份,三个英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序数,二个英文字母; 如: "th","nd"
t - 指定月份的天数; 如: "28" 至 "31"
U - 总秒数
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位数字; 如: "1999"
y - 年,二位数字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"

Add some other usages:

<?php
echo "今天:".date("Y-m-d")."
";
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "
";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "
";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "
";
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "
";
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "
";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."
";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
";
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert timestamp to date in php and make the month go to 0. 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