PHP 中的日期函数

王林
发布: 2024-08-29 12:47:10
原创
684 人浏览过

PHP 中有各种内置函数。 PHP日期函数可以轻松处理日期及其相关操作。此 date() 函数可用于根据我们的业务需求获取和设置日期和时间。 PHP time() 函数给出时间戳作为输出。函数 date() 可用于通过时间戳将日期转换为人类可读的格式。我们可以通过多种方式使用 PHP date() 函数。几乎每种编程语言都有一个内置的日期函数来处理日期和时间。在接下来的部分中,我们将通过示例看到此 PHP date() 函数的各种类型的使用。

日期函数的使用

当我们来到任何系统(在线申请)时,日期就扮演着重要的角色。对于任何业务操作和流程,我们都需要跟踪日期和时间。例如,如果我们要向系统添加库存项目,我们需要捕获日期,以便我们可以获得添加库存项目的时间。 date() 可用于将日期和时间保存在数据库中(一条记录),用于业务相关流程。日期可以通过多种方式格式化,为此我们可以传递定义的参数来获得所需的日期格式输出。我们可以在 PHP 应用程序或程序中使用 date() 函数和各种其他相关函数。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

日期函数和示例

date() 函数始终带有一个参数。

语法:

date(Param1, Param2);
登录后复制

其中Param1为必填参数,表示日期格式,Param2为时间戳,为可选参数。 Param1 是字符串,Param2 是长整数。

1.获取当前日期

代码:

<?php
echo "Date: " . date("Y/m/d"); // 2019/11/17
echo "<br>";
echo "Date: " . date("d-M-Y"); // 17-Nov-2019
?>
登录后复制

输出:

PHP 中的日期函数

请注意,我们得到的是 2019 年 11 月 17 日,因为它是在 2019 年 11 月 17 日运行的。

我们可以看到上面的程序,日期(“Y/m/d”)将给我们2019代替Y(大写字母Y),11代替m(小m),17代替d(小d)。如果我们在 2020 年 1 月 20 运行相同的 PHP 程序,我们将得到相应的输出,这意味着它将给出当前日期。

2.获取当前时间戳

PHP 中有 time() 函数来获取时间戳。时间戳什么也不是,它是从开始时间到当前时间之间的一个数值。时间戳日期固定为 1970-01-01 00:00:00(1970-01-01 0 小时 0 分 0 秒)(GMT)。

代码:

<?php
echo "Current Timestamp: ".time();
?>
登录后复制

输出:

PHP 中的日期函数

在任何时间点,我们都可以在 PHP 中获取时间戳到日期和日期到时间戳。

3.从时间戳获取日期:

在此,我们将使用日期函数的两个参数。第一个参数是我们想要作为输出的日期格式,第二个参数是时间戳本身。让我们通过一个简单的例子来看看同样的情况。

代码:

<?php
$timeStamp = 1574012580;
echo "Timestamp $timeStamp to Date is " . date("d/M/Y",$timeStamp);
?>
登录后复制

输出:

PHP 中的日期函数

4.单独打印给定时间戳的年份和月份

代码:

<?php
$timeStamp = 1574012580;
echo "Year: " . date("Y",$timeStamp);  // year
echo "<br>";
echo "Month: " . date("M",$timeStamp);  // month in 3 character format
?>
登录后复制

输出:

PHP 中的日期函数

5.从 mktime() 函数获取日期

是的,我们在 PHP 中还有另一个函数,它包含 mktime() 来处理与日期相关的内容。我们可以在 mktime() 中使用各种数字参数。 mktime() 再次将输出作为时间戳给出。我们可以使用 mktime() 函数输出作为 date() 函数的第 2nd 参数来获取日期,让我们通过一个示例来看看。

代码:

<?php
$mktime = mktime(01, 01, 59, 11, 17, 2019); //  01 = hour, 01 = minute, 59 = second, 11= month, 17 = day, 2019 = year
echo "Year: " . date("Y-M-d, h: i: sa ", $mktime);  // year-month-date
?>
登录后复制

mktime 参数值的序列 = 小时、分钟、秒、月、日和年。这个顺序是固定和预定义的,如果我们在传递参数时犯了任何错误,那么我们将不得不将日期作为输出,所以我们在处理这个函数时需要小心。

输出:

PHP 中的日期函数

6. Strtotime() function in PHP

We have another function strtotime(). This is a function; we can pass a string to get the date as per requirements. For example, we can pass today, tomorrow, yesterday, +2 days, -2 days, +5 years like that. Then we can use the output of this function to get the date and the time using the date() function. Let’s see the same with a quick example.

Code:

<?php
echo "Today: ". date("Y-m-d", strtotime("today")) . "<br>";
echo "Tomorrow: ".  date("Y-m-d", strtotime("tomorrow")) . "<br>";
echo "Yesterday: ".  date("Y-m-d", strtotime("Yesterday")) . "<br>";
echo "2 Days after today: ".  date("Y-m-d", strtotime("+2 days")) . "<br>";
echo "2 Days before today: ".  date("Y-m-d", strtotime("-2 days")) . "<br>";
echo "5 years after today: ".  date("Y-m-d", strtotime("+5 years")) . "<br>";
?>
登录后复制

Output:

PHP 中的日期函数

Apart from the above mentioned we have other various function

  • date_default_timezone_set(): This function can be used to set the time zone. Like we use Asia/Kolkata time zone in PHP for India.
  • date_default_timezone_get(): This function can be used to get the current time zone that is set in the php. We can change the time zone using the date_default_timezone_set(“time-zone”); function. We can directly pass the time zone name to set the specified time zone.

Code:

<?php
echo date_default_timezone_get(); //to get the timezone
echo "<br>";
date_default_timezone_set('Asia/Kolkata');
echo date_default_timezone_get(); //to get the timezone
?>
登录后复制

Output:

PHP 中的日期函数

Conclusion

We can simply say, this PHP date() function really plays a vital role in any online application.  It is always advisable to keep the timestamp into the database table so that we will have the flexibility that we can get the date and time from that as per our needs.

以上是PHP 中的日期函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!