首页  >  文章  >  后端开发  >  现在 PHP

现在 PHP

王林
王林原创
2024-08-29 12:59:471046浏览

在 PHP 中,now() 函数用于获取当前日期和时间。如今,使用日期和时间函数代替 now() 函数。日期和时间都是内置函数,其中日期函数用于根据用户的具体要求格式化日期和时间。它返回以特定格式显示日期的字符串。 time 函数将当前时间返回给用户,该时间以 Unix 时间戳的秒数来衡量。使用 time() 函数(相当于 now() 函数)时,它返回的值取决于默认时区。

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

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

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

现在的 PHP 语法

as now() 函数用于根据用户的要求检索日期和时间。

下面给出了 PHP 中 now() 函数获取日期的基本语法:

date(format, [ts]);

哪里,

  • 格式:它定义了我们希望根据特定要求在输出中显示日期的格式。例如,“Y”用于显示当前年份。 ‘Y-M-D’ 用于在 PHP 中以 ‘YYYY-MM-DD’ 格式显示日期。
  • ts: 此参数是可选的。它指定当前时间戳。如果用户未指定时间戳,PHP 将使用服务器上的当前日期时间。

在 PHP 中使用 now() 函数获取时间的语法如下:

time();

哪里,

  • time() 函数返回当前时间(以秒为单位)。它不接受任何参数。

now() 函数在 PHP 中如何工作?

与 PHP 中 now() 函数工作相关的一些要点如下:

  • PHP 中的默认时区在 PHP.ini 文件中设置。
  • 我们还可以在 PHP 中以编程方式设置时区。为了设置它,需要使用 date_default_timezone_set 函数。
  • 我们可以使用 listIdentifiers() 函数获取所有可用时区的列表,该函数是内置 DateTimeZone 类的静态方法。
  • 在 PHP 中,mktime() 是一个函数,用于以 Unix 格式将时间戳返回给用户。

示例:

代码:

<?PHP
mktime(hr, min, sec, mon, day, yr, is_dst);
?>
  • PHP 中使用的 now() 中的所有日期函数都使用默认时区。
  • 当前时间与 1970 年 1 月 1 日 00:00:00 GMT 之间的秒数是时间戳。这也称为 UNIX 时间戳。
  • 虽然程序员在PHP中有可以通过PHP代码设置时区的功能。

显示时间时使用的now()函数的一些常用时间参数如下:

Sr.No Parameter Description
1 H It represents the 24-hour format of an hour. It represents the hours between [00-23].
2 h It represents the 12-hour format of an hour. It represents hours between [1-12] with leading zeros.
3 s It represents the seconds with leading zeros. It represents the seconds between [00-59].
4 i It represents the minutes with leading zeros. It represents data between [00-59].
5 A, ‘a’ It represents whether the time displayed is in am or pm (Ante meridiem and Post meridiem).
6 r It is used to return the full date and time value.
老号 参数 描述 1 高 它代表一个小时的24小时格式。它代表[00-23]之间的小时。 2 高 它代表一小时的 12 小时格式。它表示 [1-12] 之间的小时,并带有前导零。 3 s 它表示带有前导零的秒。它代表[00-59]之间的秒。 4 我 它表示带有前导零的分钟。代表[00-59]之间的数据。 5 A,'a' 表示显示的时间是上午还是下午(中午前和下午后)。 6 r 用于返回完整的日期和时间值。 表>

Some of the commonly used Date parameters of now() function in the date are given below:

Sr.No Parameter Description
1 d It represents the day number in the month. The value ranges of this parameter are [01-31].
2 D It represents the day in a week. It displays the first 3 letters of the day. The value range of this parameter is [Sun- Sat].
3 z It is used to represent the day in a year. Its value ranges from [0-365].
4 l It is used to represent the weekday name. It displays the values from Sunday to Saturday.

Some of the commonly used Year parameters of now() function in the date are given below:

Sr.No Parameter Description
1 Y It represents the full 4 digits year format. The year displayed will be ‘YYYY’.
2 y It represents the 2 digits year format. The year displayed will be ‘YY’. Values range from [00-99].
3 L It represents whether the year is a leap year or not. If the year is a leap year, it returns 1. It returns 0 if the year is not a leap year and -1 if the year is unknown.

Some of the commonly used Month parameters of now() function in the date are given below:

Sr.No Parameter Description
1 M It represents the name of the month. Starting first 3 letters of the month name are displayed. The value ranges from [Jan-Dec].
2 m It represents the month number in numerical format. The value ranges from [01-12].
3 t It is used to represent the number of days in a given month. Its value ranges from [28-31] depending on the month and year given.
4 F It represents the full name of the month. The value ranges from [JANUARY -DECEMBER].

Examples of PHP now

Given below are the examples of PHP now:

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Today's date is ".date("r"). "<br>";
echo "Formatted is ".date('y-m-d')."<br>";
echo " Format 2 is ".date('Y/M/D')."<br>";
echo "Day number according to year is ".date("z");
?>
</body>
</html>

Output:

现在 PHP

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Current time in 24 hr format : " . date("H: i: s"). "<br>";
echo "Current time in 24 hr format : " . date("h -i - s"). "<br>";
echo "Check am or pm : " . date("a"). "<br>";
?>
</body>
</html>

Output:

现在 PHP

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Default time <br> ";
echo "Time is : ".date("H: i: s"). "<br>";
echo "Now Setting the timezone <br>" ;
date_default_timezone_set("Asia/Calcutta");
echo "The time is " .date("H:i:s"). "<br>";
?>
</body>
</html>

Output:

现在 PHP

Example #4

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
$day = mktime(34, 24, 4, 8, 2, 2020);
echo "Created date when hours exceed is:  " .date("Y-m-d H:i:s", $day). "<br>";
$day2 = mktime(7, 67, 65, 8, 2, 2020);
echo "Created date when minutes and seconds exceed is " .date("Y-m-d H:i:s", $day2). "<br>";
?>
</body>
</html>

Output:

现在 PHP

Conclusion

The above description clearly shows what is now() function in PHP and how they work in order to retrieve the current date and time in the format given by the user. The date and time function in PHP works similar to the now() function in MySQL. PHP date and time functions also throw E_WARNING and E_NOTICE to check the correct timezone or use system variables. So before using them in the program, the programmer needs to understand them well.

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

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