如何在 PHP 中获取当前日期和时间

在本文中,我们将介绍在 PHP 中获取当前 datetime 的方法。

  • 使用 date()time() 函数
  • 使用 DateTime 对象

使用 date()time() 函数获取 PHP 中的当前日期和时间

我们可以使用内置函数 date()time() 以获取 PHP 中的当前日期和时间。这些功能显示当前日期和时间,与时区无关。使用这两个函数的正确语法如下。

date($format, $timestamp);

内置函数 date() 有两个参数。其参数的详细信息如下

参数  描述
$format 强制性的 要显示的日期格式。它有多种变体。应该是一个字符串。
$timestamp 可选的 获取 date 的时间戳。如果省略,则返回当前日期。

它返回根据指定格式设置的日期。

time();

time() 函数不接受任何参数。它返回当前的 PHP 时间戳。时间戳是自 Unix 纪元以来计算的秒数。

下面的程序显示了如何获取当前的日期和时间,而与时区无关。

<?php
$DateAndTime = date('m-d-Y h:i:s a', time());
echo "The current date and time are $DateAndTime.";
?>

格式 "m-d-Y h:i:s a" 指定了返回的带有月,日和 4 位数字年份值的日期,以及以小时,分钟和秒为单位的时间。

输出:

The current date and time are 05-20-2020 05:44:03 pm.

这里要注意的一件事是,没有根据某个时区设置时间。它是使用 Unix 时间戳显示的时间。如果要根据时区检查当前的 PHP time,则需要手动设置时区。

例如,如果要检查阿姆斯特丹的当前时间,则必须手动设置阿姆斯特丹的时区。为此,使用了 PHP 函数 date_default_timezone_set()。使用此函数的正确语法如下

date_default_timezone_set($timezone);

它仅接受一个参数。

参数  描述
$timezone 强制性的 它是一个字符串,用于标识我们希望为其设置时区的区域。你可以在此处检查支持的时区列表。

该函数返回布尔值。如果时区设置成功,则返回 true;否则,它返回 false

设置时区后,下面的程序将显示时间。

<?php
date_default_timezone_set('Europe/Amsterdam');
$DateAndTime = date('m-d-Y h:i:s a', time());
echo "The current date and time in Amsterdam are $DateAndTime.\n";
date_default_timezone_set('America/Toronto');
$DateAndTime2 = date('m-d-Y h:i:s a', time());
echo "The current date and time in Toronto are $DateAndTime2.";
?>

输出:

The current date and time in Amsterdam are 05-20-2020 08:08:42 pm.
The current date and time in Toronto are 05-20-2020 02:08:42 pm.

使用 DateTime 对象获取 PHP 中的当前日期和时间

在 PHP 中,DateTime 类用于处理与日期 date 和时间 time 有关的问题。此类的 format() 函数以指定的格式显示日期和时间。

我们将首先创建一个 DateTime 对象,然后调用 format() 函数以显示日期和时间。

$ObjectName = new DateTime();
$ObjectName->format($format);

函数 format() 接受一个参数。

参数  描述
$format 强制性的 日期和时间的格式。应该是一个字符串。

显示当前日期和时间的示例程序如下。

<?php
$Object = new DateTime();
$DateAndTime = $Object->format("d-m-Y h:i:s a");
echo "The current date and time are $DateAndTime.";
?>

输出是具有指定格式的日期和时间。显示的时间是从 Unix Epoch 开始计算的时间。

输出:

The current date and time are 20-05-2020 06:21:06 pm.

如果要根据特定时区检查时间,则必须手动设置时区。为此,使用了 DateTimesetTimezone() 的 PHP 内置函数。使用此函数的正确语法如下。

$ObjectName = new DateTime();
$ObjectName->setTimezone(DateTimezone $timezone);

函数 setTimezone()仅接受一个参数。成功返回 DateTime 对象,失败返回 False

参数  描述
$timezone 强制性的 它是一个 DateTimezone 对象,用于指定时区。你可以在此处检查支持的时区列表。

根据特定时区显示时间的示例程序如下。

<?php
$Object = new DateTime();
$Object->setTimezone(new DateTimeZone('Europe/Amsterdam'));
$DateAndTime = $Object->format("d-m-Y h:i:s a");
echo "The current date and time in Amsterdam are $DateAndTime.\n";
$Object->setTimezone(new DateTimeZone('America/Toronto'));
$DateAndTime = $Object->format("d-m-Y h:i:s a");
echo "The current date and time in Toronto are $DateAndTime.";
?>

输出:

The current date and time in Amsterdam are 20-05-2020 08:43:02 pm.
The current date and time in Toronto are 20-05-2020 02:43:02 pm.

如何在 PHP 中获取当前年份

在本文中,我们将介绍获取当前年份的方法。

  • 使用 date() 函数
  • 使用 strftime() 函数
  • DateTime 对象中使用 format() 方法

使用 date() 函数获取 PHP 的当前年份

我们可以使用内置的 date() 函数来获取当前年份。它以指定的格式返回当前日期。其语法如下

date($format, $timestamp);

参数 $format 说明将要显示的最终日期格式。它有多种变体。它应是一个字符串形式。

参数 $timestamp 是可选的。它告知特定的时间戳,并相应显示日期。如果没有传递时间戳,则默认使用当前日期。

<?php
$Date = date("d-m-Y");
echo "The current date is $Date.";
echo "\n";
$Year = date("Y");
echo "The current year is $Year.";
echo "\n";
$Year2 = date("y");
echo "The current year in two digits is $Year2.";
?>

格式 "d-m-Y" 将返回带有 4 位数字年份值的完整日期。格式 "Y" 将返回 4 位数字的年份值。格式 "y" 将返回 2 位数的年份值。你可以根据需要进行修改。

输出:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.

使用 strftime() 函数获取 PHP 的当前年份

PHP 中的另一个内置函数-strftime() 也可以返回给定格式的日期或时间字符串。对于日期和时间的变化,它具有不同的格式。

strftime($format, $timestamp);

参数 $format 说明日期或时间的格式。它是一个字符串。

$timestamp 是一个可选变量,用于告知 UNIX 时间戳记。它是整数。如果未给出,该函数将返回当前日期或时间。

<?php
$Date = strftime("%d-%m-%Y");
echo "The current date is $Date.";
echo "\n";
$Year = strftime("%Y");
echo "The current year is $Year.";
echo "\n";
$Year2 = strftime("%y");
echo "The current year in two digits is $Year2.";
?>
警告

我们可以使用格式 "%Y" 获得当前年份的 4 位数字,使用格式 "%y" 获得 2 位数字的当前年。

输出:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.

使用 DateTime 对象获取 PHP 的当前年份

在 PHP 中,DateTimeformat() 函数用于以指定的格式显示日期。

$ObjectName = new DateTime();
$ObjectName->format($format);

参数 $format 指定日期的格式。

<?php
$Object = new DateTime();
$Date = $Object->format("d-m-Y");
echo "The current date is $Date.";
echo "\n";
$Year = $Object->format("Y");
echo "The current year is $Year.";
echo "\n";
$Year2 = $Object->format("y");
echo "The current year in two digits is $Year2.";
?>

输出是具有指定格式的日期。

输出:

The current date is 20-04-2020.
The current year is 2020.
The current year in two digits is 20.