日历扩展包含了简化不同日历格式间的转换的函数。
它是基于 Julian Day Count(儒略日计数),是从公元前 4713 年 1 月 1 日开始计算的。
为了让这些函数能够工作,您必须通过 --enable-calendar 编译 PHP。
PHP 的 Windows 版本已内建了对日历扩展的支持。因此,Calendar 函数会自动工作。
// 检查日历扩展是否加载
if (extension_loaded('calendar')) {
echo '日历扩展已加载';
} else {
echo '日历扩展未加载';
}
| 函数 | 描述 | 版本 |
|---|---|---|
| cal_days_in_month() | 针对指定的年份和历法,返回一个月中的天数。 | PHP 4.1+ |
| cal_from_jd() | 把儒略日计数转换为指定历法的日期。 | PHP 5+ |
| cal_info() | 返回有关指定历法的信息。 | PHP 5+ |
| cal_to_jd() | 把指定历法的日期转换为儒略日计数。 | PHP 5+ |
| easter_date() | 返回指定年份的复活节午夜的 Unix 时间戳。 | PHP 4+ |
| easter_days() | 返回指定年份的复活节与 3 月 21 日之间的天数。 | PHP 4+ |
| frenchtojd() | 把法国共和历法的日期转换成为儒略日计数。 | PHP 4+ |
| gregoriantojd() | 把格利高里历法的日期转换成为儒略日计数。 | PHP 4+ |
| jddayofweek() | 返回日期在周几。 | PHP 4+ |
| jdmonthname() | 返回月的名称。 | PHP 4+ |
| jdtofrench() | 把儒略日计数转换为法国共和历法的日期。 | PHP 4+ |
| jdtogregorian() | 把儒略日计数转换为格利高里历法的日期。 | PHP 4+ |
| jdtojewish() | 把儒略日计数转换为犹太历法的日期。 | PHP 4+ |
| jdtojulian() | 把儒略日计数转换为儒略历法的日期。 | PHP 4+ |
| jdtounix() | 把儒略日计数转换为 Unix 时间戳。 | PHP 5+ |
| jewishtojd() | 把犹太历法的日期转换为儒略日计数。 | PHP 4+ |
| juliantojd() | 把儒略历法的日期转换为儒略日计数。 | PHP 4+ |
| unixtojd() | 把 Unix 时间戳转换为儒略日计数。 | PHP 5+ |
| 常量 | 类型 | PHP 版本 |
|---|---|---|
| CAL_GREGORIAN | Integer | PHP 4+ |
| CAL_JULIAN | Integer | PHP 4+ |
| CAL_JEWISH | Integer | PHP 4+ |
| CAL_FRENCH | Integer | PHP 4+ |
| CAL_NUM_CALS | Integer | PHP 4+ |
| CAL_DOW_DAYNO | Integer | PHP 4+ |
| CAL_DOW_SHORT | Integer | PHP 4+ |
| CAL_DOW_LONG | Integer | PHP 4+ |
| CAL_MONTH_GREGORIAN_SHORT | Integer | PHP 4+ |
| CAL_MONTH_GREGORIAN_LONG | Integer | PHP 4+ |
| CAL_MONTH_JULIAN_SHORT | Integer | PHP 4+ |
| CAL_MONTH_JULIAN_LONG | Integer | PHP 4+ |
| CAL_MONTH_JEWISH | Integer | PHP 4+ |
| CAL_MONTH_FRENCH | Integer | PHP 4+ |
| CAL_EASTER_DEFAULT | Integer | PHP 4.3+ |
| CAL_EASTER_ROMAN | Integer | PHP 4.3+ |
| CAL_EASTER_ALWAYS_GREGORIAN | Integer | PHP 4.3+ |
| CAL_EASTER_ALWAYS_JULIAN | Integer | PHP 4.3+ |
| CAL_JEWISH_ADD_ALAFIM_GERESH | Integer | PHP 5.0+ |
| CAL_JEWISH_ADD_ALAFIM | Integer | PHP 5.0+ |
| CAL_JEWISH_ADD_GERESHAYIM | Integer | PHP 5.0+ |
// 获取格利高里历法的信息
$info = cal_info(CAL_GREGORIAN);
print_r($info);
// 计算某月的天数
$days = cal_days_in_month(CAL_GREGORIAN, 2, 2024);
echo "2024年2月有 {$days} 天";
// 将格利高里日期转换为儒略日
$jd = gregoriantojd(2, 15, 2024);
echo "儒略日计数: {$jd}";
// 将儒略日转换回格利高里日期
$gregorian = jdtogregorian($jd);
echo "格利高里日期: {$gregorian}";