如何在 PHP 中获取当前日期和时间
如何在 PHP 中获取当前日期和时间
在本文中,我们将介绍在 PHP 中获取当前 date 和 time 的方法。
使用 date() 和 time() 函数获取 PHP 中的当前日期和时间
我们可以使用内置函数 date() 和 time() 以获取 PHP 中的当前日期和时间。这些功能显示当前日期和时间,与时区无关。使用这两个函数的正确语法如下。
date($format, $timestamp);
内置函数 date() 有两个参数。其参数的详细信息如下
参数描述
$
强制性的
要显示的日期格式。它有多种变体。应该是一个字符串。
$
可选的
获取 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.

这里要注意的一件事是php 怎样获取api当前时间,没有根据某个时区设置时间。它是使用 Unix 时间戳显示的时间。如果要根据时区检查当前的 PHP time,则需要手动设置时区。
例如,如果要检查阿姆斯特丹的当前时间php 怎样获取api当前时间,则必须手动设置阿姆斯特丹的时区。为此,使用了 PHP 函数 e_set()。使用此函数的正确语法如下
date_default_timezone_set($timezone);
它仅接受一个参数。
参数描述
$
强制性的
它是一个字符串如何在 PHP 中获取当前日期和时间,用于标识我们希望为其设置时区的区域。你可以在此处检查支持的时区列表。
该函数返回布尔值。如果时区设置成功,则返回 true;否则php 怎样获取api当前时间,它返回 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.
使用 对象获取 PHP 中的当前日期和时间
在 PHP 中, 类用于处理与日期 date 和时间 time 有关的问题。此类的 () 函数以指定的格式显示日期和时间。
我们将首先创建一个 对象,然后调用 () 函数以显示日期和时间。
$ObjectName = new DateTime();
$ObjectName->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.
如果要根据特定时区检查时间如何在 PHP 中获取当前日期和时间,则必须手动设置时区。为此,使用了 类 () 的 PHP 内置函数。使用此函数的正确语法如下。
$ObjectName = new DateTime();
$ObjectName->setTimezone(DateTimezone $timezone);
函数 ()仅接受一个参数。成功返回 对象,失败返回 False。
参数描述
$
强制性的
它是一个 对象,用于指定时区。你可以在此处检查支持的时区列表。
根据特定时区显示时间的示例程序如下。
<?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.
关于作者
Noor查看作者主页 →
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299
资源库 - 资源分享下载网 » 如何在 PHP 中获取当前日期和时间
常见问题FAQ
- 关于资源售价和售后服务的说明?
- 代码有没有售后服务和技术支持?
- 有没有搭建服务?
- 链接地址失效了怎么办?
- 关于解压密码

