首页  >  文章  >  后端开发  >  PHP 中的日历

PHP 中的日历

WBOY
WBOY原创
2024-08-29 12:47:33512浏览

PHP 日历函数就是与日历相关的函数的使用。我们必须在 PHP 中使用各种内置函数,以便能够轻松执行各种与日历相关的任务。当我们在任何网络应用程序中处理事件、预订或任何类型的约会时,此日历非常有用。与使用 jQuery 创建日历相比,在 PHP 中创建日历有点困难。使用 jQuery 日历非常简单。

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

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

语法

使用日历功能的语法不仅仅局限于一行代码。有多种功能组合在一起才能使日历运行正常。有各种可用的日历类和库,如果我们想使用 PHP 版本,我们可以在 PHP 应用程序中使用。创建我们自己的具有全功能的功能将比预期花费更长的时间。

例如:如果我们想要指定月份和年份的天数,我们必须使用 cal_days_in_month(param1, param2, param2) 函数。其中 param1 是日历类型,param2 是月份编号,param3 是我们想要天数的年份。

cal_from_jd(param1, param2) 函数可以将儒略日计数转换为包含所有详细信息的日历。其中 param1 是儒略日,param2 是一种日历。

PHP 日历函数示例

有各种日历内置函数,我们将在示例部分看到各种示例。有如下功能:

示例#1

cal_days_in_month 函数: 此函数将为我们提供给定月份和年份的天数。

代码:

<?php
// to get the number of days in a calendar month
$varDays = cal_days_in_month(CAL_GREGORIAN,2,2019);
echo $varDays ." days in February 2019.";
echo "\n";
$varDays = cal_days_in_month(CAL_GREGORIAN,4,2018);
echo $varDays ." days in April 2018.";
?>

输出:

PHP 中的日历

示例#2

cal_from_jd: 此函数可用于将日历从儒略日计数转换为支持的日历。同样,我们有一个函数 cal_to_jd() 将日历转换为儒略日计数。让我们通过一个例子来理解这一点。

代码:

<?php
// to get the  calendar details of the current date
$current_timestamp = time(); // current timestamp
$unix_jd_time = unixtojd($current_timestamp);
print_r(cal_from_jd($unix_jd_time, CAL_GREGORIAN));
?>

输出:

PHP 中的日历

示例#3

cal_info:我们可以在 PHP 中使用此函数来获取有关日历的详细信息。它采用一个整数参数。  这是一个可选参数。我们需要担心参数的传递。  让我们通过示例程序来看看同样的情况。

代码:

<?php
// to get the  calendar info
$info = cal_info(0); // if we not pass any valid param then it will gives all calendar details
print_r($info);
?>

输出:

PHP 中的日历

unixtojd: 此函数可用于将时间戳转换为儒略日计数。我们已经在前面的例子中看到了。该函数接受时间戳形式的参数。

我们可以在 PHP 中使用各种其他函数来享受全功能的日历。

如何制作日历?

在 PHP 中,我们有各种类型的日历,如公历、儒略历、犹太历和法历等。在本节中,我们将了解如何使用它创建当前月份的日历。如果不使用日期和时间功能,玩日历几乎是不可能的。

<?php
function showCurrentMonth($current_Month, $year)
{
$date = mktime(12, 0, 0, $current_Month, 1, $year);
$numberOfDays =cal_days_in_month(CAL_GREGORIAN,$current_Month, $year);
$offset = date("w", $date);
$row_number = 1;
// time to draw the month header
echo "<table style='color:blue; border:1px solid blue; width:500px; height:300px;'><br/>";
echo "<tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr> <tr>";
// this will print the additional td record in case the month is not starting with the Sunday.
for($i = 1; $i <= $offset; $i++)
{
echo "<td></td>";
}
//  this will print the number of days.
for($day = 1; $day <= $numberOfDays; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "</tr> <tr>";
$row_number++;
}
echo "<td>" . $day . "</td>";
}
while( ($day + $offset) <= $row_number * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr></table>";
}
?>
<html>
<head>
<title>Calendar of the current month (Dec 2019)</title>
</head>
<body>
<p>Calendar of the Dec 2019</p>
<?php
// Dec 2019 in PHP
showCurrentMonth(11, 2019);
?>
</body>
</html>

上述代码说明:如上面的代码,我们可以看到生成一年中任意给定月份的日历的函数。我们可以根据业务需求生成任意数量的日历月。

输出:

PHP 中的日历

结论

动态日期和时间反复出现的情况下是必需的。我们也可以在 PHP 中使用 jQuery UI 日历。因此,根据截止日期和业务需求,我们也可以在 PHP 应用程序中继续使用 jQuery 日历。大多数开发人员更喜欢使用 PHP 日历之上的 jQuery UI 日历功能。 PHP 相对来说需要更多时间。

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

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