Php 简明教程

PHP - Date & Time

PHP 的内置函数库具有广泛的函数,可帮助以编程方式处理和操作日期和时间信息。可以使用日期/时间信息的字符串形式或者从当前系统的时间创建 PHP 中的 Date 和 Time 对象。

The built-in library of PHP has a wide range of functions that helps in programmatically handling and manipulating date and time information. Date and Time objects in PHP can be created by passing in a string presentation of date/time information, or from the current system’s time.

PHP 提供定义了许多方法的 DateTime 类。在本章中,我们将会详细了解 PHP 中提供的各种与日期和时间相关的。

PHP provides the DateTime class that defines a number of methods. In this chapter, we will have a detailed view of the various Date and Time related methods available in PHP.

PHP 中的日期/时间功能实现了 ISO 8601 日历,它实现了在格里高利历之前通用的现行的闰年规则。日期和时间信息内部存储为 64 位数字。

The date/time features in PHP implements the ISO 8601 calendar, which implements the current leap-day rules from before the Gregorian calendar was in place. The date and time information is internally stored as a 64-bit number.

Getting the Time Stamp with time()

PHP 的 time() 函数提供了有关当前日期和时间的所需的所有信息。它不需要参数,但返回一个整数。

PHP’s time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer.

time(): int

time() 返回的整数表示自 1970 年 1 月 1 日格林尼治标准时间午夜以来的经过的秒数。这个时刻称为 UNIX 紀元,并且从那时起经过的秒数称为时间戳。

The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970. This moment is known as the UNIX epoch, and the number of seconds that have elapsed since then is referred to as a time stamp.

<?php
   print time();
?>

它将生成以下 output

It will produce the following output

1699421347

我们可以将时间戳转换为人类容易理解的形式。

We can convert a time stamp into a form that humans are comfortable with.

Converting a Time Stamp with getdate()

该函数 getdate() 可以选择接受一个时间戳,并返回一个包含有关日期信息的关联数组。如果您省略时间戳,它会使用 time() 返回的当前时间戳。

The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().

下表列出了 getdate() 返回的数组中包含的元素。

The following table lists the elements contained in the array returned by getdate().

Sr.No

Key & Description

Example

1

seconds Seconds past the minutes (0-59)

20

2

minutes Minutes past the hour (0 - 59)

29

3

hours Hours of the day (0 - 23)

22

4

mday Day of the month (1 - 31)

11

5

wday Day of the week (0 - 6)

4

6

mon Month of the year (1 - 12)

7

7

year Year (4 digits)

1997

8

yday Day of year ( 0 - 365 )

19

9

weekday Day of the week

Thursday

10

month Month of the year

January

11

0 Timestamp

948370048

现在您可以完全控制日期和时间。您可以按照想要的任何格式格式化此日期和时间。

Now you have complete control over date and time. You can format this date and time in whatever format you want.

Example

看看下面的 example

Take a look at this following example

<?php
   $date_array = getdate();

   foreach ( $date_array as $key => $val ){
      print "$key = $val\n";
   }
   $formated_date  = "Today's date: ";
   $formated_date .= $date_array['mday'] . "-";
   $formated_date .= $date_array['mon'] . "-";
   $formated_date .= $date_array['year'];

   print $formated_date;
?>

它将生成以下 output

It will produce the following output

seconds = 0
minutes = 38
hours = 6
mday = 8
wday = 3
mon = 11
year = 2023
yday = 311
weekday = Wednesday
month = November
0 = 1699421880
Today's date: 8-11-2023

Converting a Time Stamp with date()

date() 函数返回一个表示日期的格式化字符串。通过要传递给它的字符串参数,可以对 date() 返回的格式进行大量的控制。

The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument that you must pass to it.

date(string $format, ?int $timestamp = null): string

date() 可选择接受时间戳,如果省略则使用当前日期和时间。包含在传递给 date() 的格式化字符串中的任何其他数据都将包含在返回值中。

The date() optionally accepts a time stamp if omitted then current date and time will be used. Any other data you include in the format string passed to date() will be included in the return value.

下表列出了格式化字符串可以包含的代码:

The following table lists the codes that a format string can contain −

Sr.No

Format & Description

Example

1

a 'am' or 'pm' lowercase

pm

2

A 'AM' or 'PM' uppercase

PM

3

d Day of month, a number with leading zeroes

20

4

D Day of week (three letters)

Thu

5

F Month name

January

6

h Hour (12-hour format - leading zeroes)

12

7

H Hour (24-hour format - leading zeroes)

22

8

g Hour (12-hour format - no leading zeroes)

12

9

G Hour (24-hour format - no leading zeroes)

22

10

i Minutes ( 0 - 59 )

23

11

j Day of the month (no leading zeroes

20

12

l (Lower 'L') Day of the week

Thursday

13

L Leap year ('1' for yes, '0' for no)

1

14

m Month of year (number - leading zeroes)

1

15

M Month of year (three letters)

Jan

16

r The RFC 2822 formatted date

Thu, 21 Dec 2000 16:01:07 +0200

17

n Month of year (number - no leading zeroes)

2

18

s Seconds of hour

20

19

U Time stamp

948372444

20

y Year (two digits)

06

21

Y Year (four digits)

2006

22

z Day of year (0 - 365)

206

23

Z Offset in seconds from GMT

+5

Example

看看下面的 example

Take a look at this following example

<?php
   print date("m/d/y G.i:s \n", time()) . PHP_EOL;
   print "Today is ";
   print date("j of F Y, \a\\t g.i a", time());
?>

它将生成以下 output

It will produce the following output

11/08/23 11.23:08

Today is 8 2023f November 2023, at 11.23 am

希望您对如何根据您的要求格式化日期和时间的了解很好。供您参考,所有日期和时间函数的完整列表中给出了 PHP Date & Time Functions

Hope you have good understanding on how to format date and time according to your requirement. For your reference a complete list of all the date and time functions is given in PHP Date & Time Functions.