首页  >  文章  >  后端开发  >  PHP 时间

PHP 时间

WBOY
WBOY原创
2024-08-29 12:54:571075浏览

PHP 编程语言中有各种函数来处理日期和日期相关任务,strtotime() 就是其中之一。 PHP 语言中日期有多种用途。函数 strtotime() 是 PHP 编程语言的内置函数。该函数有两个参数,其中一个是必需的,一个是可选的。我们可以将字符串作为必需参数传递,以根据我们的业务需求处理日期。

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

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

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

语法:

我们可以通过多种方式使用 strtotime() 函数。

strtotime(DateTime, Now);
  • 第一个参数(DateTime)是字符串格式的日期/时间。此参数是必需的,我们在使用此函数时不能跳过。
  • 第二个参数(Now)是可选参数。
  • PHP 从 4+ 版本开始就支持此功能。到目前为止,这个函数已经做了各种扩展。

PHP strtotime 是如何工作的?

该函数是PHP语言中的基本函数之一。使用此功能之前不需要额外的插件或扩展。我们只需遵循该函数的语法即可根据我们的业务需求开始使用它。

例如:如果有人想知道当前日期(意味着今天是哪一天)我们可以使用此函数。为此,首先,我们必须获取当前时间戳,然后我们可以使用其他日期函数从该时间戳获取确切的日期。

代码:

<?php
$currentTimeStamp  =strtotime("now");
echo "The output at the time of writing this article was: " .$currentTimeStamp;
?>

这行代码将为我们提供当前时间戳。

输出:

PHP 时间

现在,我们将使用另一个函数从该时间戳获取人类可读的日期。

代码:

<?php
$current_date = date('m/d/Y', $currentTimeStamp);
echo "\n Today is: " . $current_date;
?>

输出:

PHP 时间

PHP strtotime 示例

下面给出了提到的示例:

示例#1

这里我们首先将上面的代码组合成一个程序并查看输出。

代码

<?php
$currentTimeStamp  =strtotime("now"); // this line will gives us the current timestamp
echo "The current timestamp is: ". $currentTimeStamp;
$current_date = date('m/d/Y', $currentTimeStamp); // converting the timestamp into the readable format
echo "\n Today is: " . $current_date;
?>

输出:

PHP 时间

请注意,此输出是在编写和运行该程序时的。我们将看到当前日期的不同输出。

示例#2

将日期更改为时间戳。我们知道,将字符串日期更改为时间戳是 strtotime() 函数的主要功能。

代码:

<?php
$date = '27/02/2010';
echo "Actual Date: ". $date."\n";
$date = str_replace('/', '-', $date);
// The nature of the strtotime() function is it takes the string date as a parameter in a specified format only.
// So we are converting the / with the - before passing it to this function we are talking about.
echo "After changing to format of the specified Date: ". $date."\n";
// Now changing the date again to the human-readable form with the different format...
echo "This is Date: ". date('Y-m-d', strtotime($date));
?>

输出:

PHP 时间

示例 #3

将字符串日期更改为实际日期。

代码:

<?php
echo strtotime("today") . "\n"; // this will give us the timestamp of today
echo strtotime("27 Mar 2020") . "\n";  // this will give us the timestamp of 27 Mar 2020
echo strtotime("+2 hours") . "\n"; // Timestamp of 2 hours later from now
echo strtotime("2 hours") . "\n"; // This will also give the Timestamp of 2 hours later from now
echo strtotime("-2 hours") . "\n"; // This will  give the Timestamp of 2 hours before from now
echo strtotime("3 week") . "\n";   // Timestamp of 3 weeks later from now
echo strtotime("last Monday") . "\n" ;      // This will give the Timestamp of the last Monday
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
?>

运行上面代码时的输出,你会看到不同的输出。这完全取决于我们何时运行这个程序。

输出:

PHP 时间

示例#4

将上面的程序修改为人类可读的日期以及这些时间戳。另外,我将当前时区设置为亚洲/加尔各答,以便我们可以根据印度的角度获取日期。我们可以根据需要将时区设置为任何时区。

代码:

<?php
date_default_timezone_set('Asia/Kolkata');
echo strtotime("today") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("today"))."\n";
echo strtotime("27 Mar 2020") ;
echo " Today is -  " .date('Y-m-d H:i:s', strtotime("27 Mar 2020"))."\n";
echo strtotime("+2 hours") ;
echo " After 2 hours from now -  " .date('Y-m-d H:i:s', strtotime("+2 hours"))."\n";
echo strtotime("-2 hours") ;
echo " Before 2 hours from now -   " .date('Y-m-d H:i:s', strtotime("-2 hours"))."\n";
echo strtotime("last Monday") ;
echo " Date and Time of Last Monday " .date('Y-m-d H:i:s', strtotime("last Monday"))."\n";
echo strtotime("next Monday");      // This will give the Timestamp of the coming Monday
echo " Date and Time of coming Monday  " .  date('Y-m-d H:i:s', strtotime("next Monday"))."\n";
?>

输出:

PHP 时间

结论

我们可以在需要时使用 strtotime() PHP 内置函数。开发人员或编码人员必须了解指定的字符串日期作为参数。每当我们需要将字符串日期更改为 Unix 时间戳时,我们都可以使用此函数。

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP addslashes()下一篇:PHP strpos