Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on the method of converting PHP timestamp to date time

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the "PHP timestamp and date-time conversion method tutorial" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

In the previous article ("Summary of methods for obtaining time in PHP (detailed examples)"), we introduced how to obtain time, that is, timestamp, so how can we complete the conversion between timestamp and date-time in PHP?

Date time is converted to time stamp

We can get the timestamp through the time () function, but the time () function can only get the timestamp of the current time in different time zones, as shown in the following example:

Output result:

What should we do when we want to convert a specific time into a timestamp? At this point, we can use the strtotime () function to do this.

Strtotime () function

The strtotime () function converts any string date described in English text into a UNIX timestamp, and its syntax format is as follows:

Strtotime (time in string [, parameter used to calculate the return value])

Examples are as follows:

Output result:

From the above example, we can see that the problem of specific time transfer timestamp is achieved through the strtotime function, and then we introduce a more intuitive function that can convert specific time into timestamp.

Mktime () function

The mktime () function returns the timestamp of a specified time and is more intuitive than the strtotime function. Its syntax format is as follows:

Mktime (specified time, specified time minutes, specified time seconds, specified time month, specified time day, specified time year)

Examples are as follows:

Output result:

Get the UNIX timestamp for 15:00:00 on October 21, 2021, with the following example:

Output result:

From the above example, you can also use the mktime function to convert time and date into a timestamp.

The above describes the function method of converting a date into a timestamp, so let's take a look at how the timestamp is converted into the time and date we are used to.

Timestamp conversion date time

Time stamp conversion to date time needs to use the date () function, this function is used to format time and date, the formatting process can also be seen as a conversion process. The syntax format of the date () function is as follows:

Date (time format after conversion, timestamp before conversion)

Among them, there are many kinds of time formats after conversion. First, I would like to list some of them:

Hmurmuri-hours in 24-hour format, 0 00 to 23

MMMI-use two-digit numbers to represent the current month 01 to 12

DMeltel-indicates the day of the month, which should be expressed as two digits. If it is less than two digits, it should be preceded by a zero.

Ymurmuri-use 4 digits to represent the full year

Smurmuri-use two digits to represent seconds, 0 00 to 59

IMurray-use two digits to represent minutes, 0 00 to 59

Let's experiment with the timestamp of 1634799600, which is the 15:00:00 timestamp of October 21, 2021 in the previous example.

Examples are as follows:

Output result:

As you can see, the date function can output a normal date and time, but it is still not in line with our daily habits. We just need to combine the parameters together. The example is as follows:

Output result:

As a result, we have completed the conversion of timestamp to date and time.

This is the end of the tutorial on the method of converting PHP timestamp to date and time. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report