Home > Backend Development > PHP Problem > PHP date and time application 14: Convert numbers to month names

PHP date and time application 14: Convert numbers to month names

藏色散人
Release: 2023-03-12 08:02:02
Original
2727 people have browsed it

Hello, this article will continue to bring you the PHP date and time series. In the previous article "PHP Date and Time Application Thirteen: Calculating the Number of Weeks Between Two Dates", I introduced to you how to calculate two dates. Friends who need it can read and understand the number of weeks between dates~

▎Related recommendations: "PHP date and time application summary (continuously updated~)"

The important content of this article is to teach you how to convert numbers into month names (English) by writing a PHP script.

Attachment: January: January; February: February; March: March; April: April; May: May; June: June; July: July; August: August ; September: September; October: October; November: November; December: December.

Let’s go directly to the code:

The PHP code is as follows:

<?php
$month_num  = 9;
$dateObj   = DateTime::createFromFormat(&#39;!m&#39;, $month_num);
$month_name = $dateObj->format(&#39;F&#39;);
echo $month_name."<br>";
Copy after login

The output result is:

September
Copy after login

In the above example we gave a number 9 and the output month name is September.

It’s very simple!

Then what we need to master is:

DateTime::createFromFormat -- date_create_from_format: Parse the date and time string according to the given format;

date_create_from_format() Function: Returns a new DateTime object formatted according to the specified format.

The syntax is: date_create_from_format(format, time, timezone);

参数	
format必需,规定要使用的格式。
--------------------------------------
format 参数字符串可以使用下列的字符:
d - 一个月中的第几天,带前导零
j - 一个月中的第几天,不带前导零
D - 一周中的某天(Mon - Sun)
I - 一周中的某天(Monday - Sunday)
S - 一个月中的第几天的英语后缀(st, nd, rd, th)
F - 月份名称(January - December)
M - 月份名称(Jan - Dec)
m - 月份(01 - 12)
n - 月份(1 - 12)
Y - 年份(例如 2013)
y - 年份(例如 13)
a 和 A - am 或 pm
g - 12 小时制,不带前导零
h - 12 小时制,带前导零
G - 24 小时制,不带前导零
H - 24 小时制,带前导零
i - 分,带前导零
s - 秒,带前导零
u - 微秒(多达六个数字)
e、O、P 和 T - 时区标识符
U - 自 Unix 纪元以来经过的秒数
(空格)
# - 下列分隔符之一:;、:、/、.、,、-、(、)
? - 一个随机字节
* - 随机字节直到下一个分隔/数字
! - 重置所有字段到 Unix 纪元
| - 如果所有字段都还没被解析,则重置所有字段到 Unix 纪元
+ - 如果存在,字符串中的尾随数据将导致警告,不是错误
---------------------------------------------------------
time必需:规定日期/时间字符串。NULL 指示当前的日期/时间。
---------------------------------------------------------
timezone可选:规定 time 的时区。默认为当前时区。
Copy after login

Finally, I would like to recommend the latest and most comprehensive "PHP video tutorial 》~Come and learn!

The above is the detailed content of PHP date and time application 14: Convert numbers to month names. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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