Home  >  Article  >  Backend Development  >  How to format time and date in PHP? How to calculate the time difference?

How to format time and date in PHP? How to calculate the time difference?

WBOY
WBOYOriginal
2021-10-21 14:24:483138browse

In the previous article, I brought you " Summary of methods of obtaining time in PHP (detailed examples) ", which introduced you in detail 5 methods of obtaining time in PHP. This article In this article, we will continue to take a look at how to format the time and date in PHP and how to calculate the time difference. I hope it will be helpful to everyone!

How to format time and date in PHP? How to calculate the time difference?

In PHP, UNIX can be used as the standard format for timestamps, but it is not easy to read. A string does not conform to our reading habits. At this time, we need to format the UNIX timestamp into the way we are accustomed to reading. The formatting can also be changed into the format required by other software.

<span style="font-size: 20px;"><strong>date()</strong></span>Time and date formatting

In PHP, we have the date() function for formatting a time or date. Its function syntax is as follows:

date(格式化后的时间格式,待格式化的时间戳)

Among them, the time format after formatting can be changed by having a special The meaning is represented by characters. The example is as follows:

d---Indicates the day of the month. It should be expressed with two digits. If it is less than two digits, add a 0 in front of it. .

<?php
    // 设定要用的时区
    date_default_timezone_set(&#39;Asia/ShangHai&#39;);
    echo date("d");
?>

Today is the 21st, the output result is:

How to format time and date in PHP? How to calculate the time difference?

##D---the abbreviation of the English day of the week.

<?php
    // 设定要用的时区
    date_default_timezone_set(&#39;Asia/ShangHai&#39;);
    echo date("d");
?>

Today is Thursday, the output result is:


How to format time and date in PHP? How to calculate the time difference?

F---English words representing the month

<?php
    // 设定要用的时区
    date_default_timezone_set(&#39;Asia/ShangHai&#39;);
    echo date("F");
?>

Today is October, the output result is:


How to format time and date in PHP? How to calculate the time difference?

There are many special characters, here are some:

t---Specify the number of days in the month 28 to 31

M---The English abbreviation of the month Jan to Dec

n---Use numbers to represent the current month 1 to 12

g---Use 12-hour format to represent the hours 1 to 12

G---Use 24-hour format to represent hours 0 to 23

z---Use numbers to represent days of the year 0 to 365

m---Use two digits to represent the current month 01 to 12

s---Use two digits to represent the seconds, with leading zeros 00 to 59>

i---Use two digits to represent minutes, with leading zeros 00 to 59>

h-- -Use 12-hour format to represent hours, with leading zeros 01 to 12

H---Use 24-hour format to represent hours, with leading zeros 00 to 23

N---Use numbers to represent the day of the week, where 1 represents Monday and 7 represents Sunday.

w---Use numbers to represent the day of the week 0 (for Sunday) to 6 (for Saturday)

can be combined together, examples are as follows:


<?php
    // 设定要用的时区
    date_default_timezone_set(&#39;Asia/ShangHai&#39;);
    echo date("t");
    echo "<br/>";
    echo date("M");
    echo "<br/>";
    echo date("n");
    echo "<br/>";
    echo date("g");
    echo "<br/>";
    echo date("G");
    echo "<br/>";
    echo date("z");
    echo "<br/>";
    echo date("m");
    echo "<br/>";
    echo date("s");
?>

Output result:


How to format time and date in PHP? How to calculate the time difference?

After the above example, we know that we can adjust the time through different parameters in the date function Format.


Next let’s learn how to calculate the time difference through PHP, which can be used as an expanded knowledge.

PHP calculates the time difference

During the development process, you may encounter situations such as how long it has been since you created this user, login How old is the website or calculating birthdays. When this happens, we need to calculate the time difference between the two times.


In PHP, we can calculate the time difference by converting two dates into timestamps. At this time, we need to use the mktime function mentioned in our previous article. (Click here "

Summary of methods of obtaining time in PHP (detailed examples)" to see the usage of the mktime function in the previous article)

Examples are as follows:

<?php
//2000年3月26日0点0分0秒
$a = mktime(0,0,0,3,26,2000);
//2021年10月21日14点10分0秒
$b = mktime(14,10,0,10,21,2021);
$diff_seconds = $b - $a;
//一周的秒数是 24*60*60*7=604800 秒
$diff_weeks = floor($diff_seconds/604800);
//一天的秒数是 24*60*60=86400
$diff_days = floor($diff_seconds/86400);
//一小时的秒数是 60*60=3600
$diff_hours = floor($diff_seconds/3600);
//一分钟的秒数是 60
$diff_minutes = floor($diff_seconds/60);
echo "2000-3-26 0:0:0 和 2021-10-21 14:10:0 之间相差:<br />".
    "$diff_seconds 秒<br />".
    "$diff_weeks 个星期<br />".
    "$diff_days 天<br />".
    "$diff_hours 个小时<br />".
    "$diff_minutes 分钟<br />";
?>

Output result:

How to format time and date in PHP? How to calculate the time difference?

#In this way, we have completed how to calculate the time difference between two times.

If you are interested, you can click on "

PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to format time and date in PHP? How to calculate the time difference?. 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