2024 年日本 PHP 研討會

日曆函式

目錄

  • cal_days_in_month — 傳回指定年份和日曆中某月份的天數
  • cal_from_jd — 將儒略日計數轉換為支援的日曆
  • cal_info — 傳回特定日曆的資訊
  • cal_to_jd — 將支援的日曆轉換為儒略日計數
  • easter_date — 取得指定年份復活節午夜的 Unix 時間戳記
  • easter_days — 取得指定年份復活節是在 3 月 21 日之後的第幾天
  • frenchtojd — 將法國共和曆日期轉換為儒略日數
  • gregoriantojd — 將格里曆日期轉換為儒略日數
  • jddayofweek — 傳回星期幾
  • jdmonthname — 傳回月份名稱
  • jdtofrench — 將儒略日數轉換為法國共和曆日期
  • jdtogregorian — 將儒略日數轉換為格里曆日期
  • jdtojewish — 將儒略日數轉換為猶太曆日期
  • jdtojulian — 將儒略日數轉換為儒略曆日期
  • jdtounix — 將儒略日轉換為 Unix 時間戳記
  • jewishtojd — 將猶太曆日期轉換為儒略日數
  • juliantojd — 將儒略曆日期轉換為儒略日數
  • unixtojd — 將 Unix 時間戳記轉換為儒略日
新增註解

使用者貢獻的註解 8 則註解

20
david dot scourfield at llynfi dot co dot uk
17 年前
我之前寫了這個函式,最近又需要用到它,所以不得不翻遍一些舊檔案才能找到它。我想把它貼在這裡,以防其他人覺得它有用。

<?php

/*
* Function to calculate which days are British bank holidays (England & Wales) for a given year.
*
* Created by David Scourfield, 07 August 2006, and released into the public domain.
* Anybody may use and/or modify this code.
*
* USAGE:
*
* array calculateBankHolidays(int $yr)
*
* ARGUMENTS
*
* $yr = 4 digit numeric representation of the year (eg 1997).
*
* RETURN VALUE
*
* Returns an array of strings where each string is a date of a bank holiday in the format "yyyy-mm-dd".
*
* See example below
*
*/

function calculateBankHolidays($yr) {

$bankHols = Array();

// New year's:
switch ( date("w", strtotime("$yr-01-01 12:00:00")) ) {
case
6:
$bankHols[] = "$yr-01-03";
break;
case
0:
$bankHols[] = "$yr-01-02";
break;
default:
$bankHols[] = "$yr-01-01";
}

// Good friday:
$bankHols[] = date("Y-m-d", strtotime( "+".(easter_days($yr) - 2)." days", strtotime("$yr-03-21 12:00:00") ));

// Easter Monday:
$bankHols[] = date("Y-m-d", strtotime( "+".(easter_days($yr) + 1)." days", strtotime("$yr-03-21 12:00:00") ));

// May Day:
if ($yr == 1995) {
$bankHols[] = "1995-05-08"; // VE day 50th anniversary year exception
} else {
switch (
date("w", strtotime("$yr-05-01 12:00:00"))) {
case
0:
$bankHols[] = "$yr-05-02";
break;
case
1:
$bankHols[] = "$yr-05-01";
break;
case
2:
$bankHols[] = "$yr-05-07";
break;
case
3:
$bankHols[] = "$yr-05-06";
break;
case
4:
$bankHols[] = "$yr-05-05";
break;
case
5:
$bankHols[] = "$yr-05-04";
break;
case
6:
$bankHols[] = "$yr-05-03";
break;
}
}

// Whitsun:
if ($yr == 2002) { // exception year
$bankHols[] = "2002-06-03";
$bankHols[] = "2002-06-04";
} else {
switch (
date("w", strtotime("$yr-05-31 12:00:00"))) {
case
0:
$bankHols[] = "$yr-05-25";
break;
case
1:
$bankHols[] = "$yr-05-31";
break;
case
2:
$bankHols[] = "$yr-05-30";
break;
case
3:
$bankHols[] = "$yr-05-29";
break;
case
4:
$bankHols[] = "$yr-05-28";
break;
case
5:
$bankHols[] = "$yr-05-27";
break;
case
6:
$bankHols[] = "$yr-05-26";
break;
}
}

// Summer Bank Holiday:
switch (date("w", strtotime("$yr-08-31 12:00:00"))) {
case
0:
$bankHols[] = "$yr-08-25";
break;
case
1:
$bankHols[] = "$yr-08-31";
break;
case
2:
$bankHols[] = "$yr-08-30";
break;
case
3:
$bankHols[] = "$yr-08-29";
break;
case
4:
$bankHols[] = "$yr-08-28";
break;
case
5:
$bankHols[] = "$yr-08-27";
break;
case
6:
$bankHols[] = "$yr-08-26";
break;
}

// Christmas:
switch ( date("w", strtotime("$yr-12-25 12:00:00")) ) {
case
5:
$bankHols[] = "$yr-12-25";
$bankHols[] = "$yr-12-28";
break;
case
6:
$bankHols[] = "$yr-12-27";
$bankHols[] = "$yr-12-28";
break;
case
0:
$bankHols[] = "$yr-12-26";
$bankHols[] = "$yr-12-27";
break;
default:
$bankHols[] = "$yr-12-25";
$bankHols[] = "$yr-12-26";
}

// Millenium eve
if ($yr == 1999) {
$bankHols[] = "1999-12-31";
}

return
$bankHols;

}

/*
* EXAMPLE:
*
*/

header("Content-type: text/plain");

$bankHolsThisYear = calculateBankHolidays(2007);

print_r($bankHolsThisYear);

?>

將輸出此結果

陣列
(
[0] => 2007-01-01
[1] => 2007-04-06
[2] => 2007-04-09
[3] => 2007-05-07
[4] => 2007-05-28
[5] => 2007-08-27
[6] => 2007-12-25
[7] => 2007-12-26
)
6
obaida dot habboush at gmail dot com
7 年前
您好,

函式名稱有個錯誤

在 HijriCalendar 類別中 {
.
.
.
.
.
.
函式 HijriToGregorian($m, $d, $y)
{
//// jd_to_cal -> 應該改為 -> cal_to_jd
return jd_to_cal(CAL_GREGORIAN, HijriCalendar::HijriToJD($m, $d, $y));
}
}
1
jthome at fcgov dot com
21 年前
我遇到了與 curlee 類似的問題,只是我需要建立一個 JDE_ERP 日期。[格式為 CYYDDD]

<?php

函式 jde_date_create($month, $day, $year){
/*
* 注意:$month 和 $day 不能有前導零,
* $year 必須是 'YYYY' 格式
*/
$jde_year_prefix = substr($year, 0, 1) - 1;
$jde_year_suffix = substr($year, -2);

//請注意 mktime 的有效年份是 1902-2037
$timestamp = mktime(0,0,0,$month, $day, $year);
$baseline_timestamp = mktime(0,0,0,1,0,$year);

$day_count = round(($timestamp - $baseline_timestamp)/86400);
$day_count_padded = str_pad($day_count,3,"0",STR_PAD_LEFT);

return (
$jde_year_prefix . $jde_year_suffix . $day_count_padded);

}

echo
jde_date_create(6,25,2000);// 將返回 '103176'

?>

--
Jim
0
simon at chronolabs dot org dot au
17 年前
這是埃及曆法,它是地球上第一個 365 天的曆法,它沒有閏年,這使得它有點不準確,但你可以在你喜歡的時候修改這個程式碼,這是另一個 roun 浮點程式碼的實現,這是一個用於產生日期編解碼器的通用程式碼,你幾乎可以用這個程式碼製作所有日曆,$pweight 是基於我對埃及紀元或 PPO 碳定年的研究,有些人認為是幾千年,而其他人則認為埃及紀元大約在 30000 年前!!這是基於雕像和紀念碑上的潮汐標記,當該區域在建造後因海平面上升而被水覆蓋時。

<?php

print_r
(EgyptCalendar(time(),0));

function
EgyptianCalendar($unix_time, $gmt,
$poffset = '1970-02-26 7:45 PM',
$pweight = '-9777600.22222222223',
$defiency='nonedeficient',
$timeset= array("hours" => 24,
"minutes" => 60,
"seconds" => 60))
{
// Code Segment 1 – Calculate Floating Point
$tme = $unix_time;

if (
$gmt>0){$gmt=-$gmt;} else {$gmt=abs($gmt);}

$ptime = strtotime($poffset)+(60*60*gmt);
$weight = $pweight+(1*gmt);

$egypt_xa = ($tme)/(24*60*60);
$egypt_ya = $ptime/(24*60*60);
$egypt = (($egypt_xa -$egypt_ya) -
$weight)+(microtime/999999);

// Code Segment 2 – Set month day arrays
$nonedeficient = array(
"seq1" => array(30,30,30,30,30,30,30,30,30,30,30,30,5));

$monthnames = array(
"seq1" => array('Thoth','Phaophi','Athyr','Choiak',
'Tybi', 'Mecheir','Phamenoth','Pharmuthi','Pachon',
'Payni','Epiphi','Mesore','epagomenai'));

$monthusage = isset($defiency) ? ${$defiency} : $deficient;

// Code Segment 3 – Calculate month number, day number
foreach($monthusage as $key => $item){
$i++;
foreach(
$item as $numdays){
$ttl_num=$ttl_num+$numdays;
$ttl_num_months++;
}
}

$revolutionsperyear = $ttl_num / $i;
$numyears = egyptd((floor($egypt) / $revolutionsperyear),0);
$avg_num_month = $ttl_num_months/$i;
$jtl = abs(abs($egypt) -
ceil($revolutionsperyear*($numyears+1)));

while(
$month==0){
$day=0;
$u=0;
foreach(
$monthusage as $key => $item){
$t=0;
foreach(
$item as $numdays){
$t++;
$tt=0;
for(
$sh=1;$sh<=$numdays;$sh++){
$ii=$ii+1;
$tt++;
if (
$ii==floor($jtl)){
if (
$egypt>0){
$daynum = $tt;
$month = $t;
} else {
$daynum = $numdays-$tt;
$month = $avg_num_month-$t;
}
$sequence = $key;
$nodaycount=true;
}
}
if (
$nodaycount==false)
$day++;
}
$u++;
}
}

//$numyears = abs($numyears);

$timer = substr($egypt, strpos($egypt,'.')+1,
strlen($egypt)-strpos($egypt,'.')-1);
$egypt_out= $numyears.'/'.$month.'/'.$daynum.' '.$day.'.'.
floor(intval(substr($timer,0,2))/100*$timeset['hours']).':'.
floor(intval(substr($timer,2,2))/100*$timeset['minutes']).':'.
floor(intval(substr($timer,4,2))/100*$timeset['seconds']).'.'.
substr($timer,6,strlen($timer)-6);
$egypt_obj = array('year'=>$numyears,
'month'=>$month,
'mname' => $monthnames[$sequence][$month-1],
'day'=>$daynum,
'jtl'=>$jtl,
'day_count'=>$day,
'hours'=>floor(intval(substr($timer,0,2))/100
*$timeset['hours']),
'minute'=>floor(intval(substr($timer,2,2))/100
*$timeset['minutes']),
'seconds'=>floor(intval(substr($timer,4,2))/100
*$timeset['seconds']),
'microtime'=>substr($timer,6,strlen($timer)-6),
'strout'=>$egypt_out);

return
$egypt_obj;
}

?>

順便說一下,在我之前發布的瑪雅蒂卡爾曆法中,函式的頂部在我的程式碼複製貼上中被截斷了,也就是函式的程式碼行,看起來像,前三行需要替換為

<?php

print_r
(MayanTikalCalendar(time(),0));

函式
MayanTikalCalendar($unix_time, $gmt,

?>
-5
amichauer at gmx dot de
19 年前
<?php

class HijriCalendar
{
function
monthName($i) // $i = 1..12
{
static
$month = array(
"M?x?rr?m", "Safar", "Rabig-?l-?ww?l", "Rabig-?l-Ax?r",
"C?m?d-?l-?ww?l", "C?m?d-?l-Ax?r", "Rac?b", "???b?n",
"Ramazan", "??w?l", "Z?-?l-Q??d?", "Z?-?l-Xicc?"
);
return
$month[$i-1];
}

function
GregorianToHijri($time = null)
{
if (
$time === null) $time = time();
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);

return
HijriCalendar::JDToHijri(
cal_to_jd(CAL_GREGORIAN, $m, $d, $y));
}

function
HijriToGregorian($m, $d, $y)
{
return
jd_to_cal(CAL_GREGORIAN,
HijriCalendar::HijriToJD($m, $d, $y));
}

# Julian Day Count To Hijri
function JDToHijri($jd)
{
$jd = $jd - 1948440 + 10632;
$n = (int)(($jd - 1) / 10631);
$jd = $jd - 10631 * $n + 354;
$j = ((int)((10985 - $jd) / 5316)) *
((int)(
50 * $jd / 17719)) +
((int)(
$jd / 5670)) *
((int)(
43 * $jd / 15238));
$jd = $jd - ((int)((30 - $j) / 15)) *
((int)((
17719 * $j) / 50)) -
((int)(
$j / 16)) *
((int)((
15238 * $j) / 43)) + 29;
$m = (int)(24 * $jd / 709);
$d = $jd - (int)(709 * $m / 24);
$y = 30*$n + $j - 30;

return array(
$m, $d, $y);
}

# Hijri To Julian Day Count
function HijriToJD($m, $d, $y)
{
return (int)((
11 * $y + 3) / 30) +
354 * $y + 30 * $m -
(int)((
$m - 1) / 2) + $d + 1948440 - 385;
}
};

$hijri = HijriCalendar::GregorianToHijri( time() );
echo
$hijri[1].'. '.HijriCalendar::monthName($hijri[0]).' '.$hijri[2];

?>
-2
simon at chronolabs dot org dot au
17 年前
<?php

// 您需要在 EgyptianCalendar 函式中替換此區段
// 以及 MayanTihkalCalendar 函式
$revolutionsperyear = $ttl_num / $i;
$numyears = floor((ceil($roun) / $revolutionsperyear));
$avg_num_month = $ttl_num_months/$i;
$jtl = abs(abs($roun) - ceil($revolutionsperyear*($numyears+1)));
while(
$month==0){
$day=0;
$u=0;
foreach(
$monthusage as $key => $item){
$t=0;
foreach(
$item as $numdays){
$t++;
$tt=0;
for(
$sh=1;$sh<=$numdays;$sh++){
$ii=$ii+1;
$tt++;
if (
$ii==floor($jtl)){
if (
$roun<0){
$daynum = $tt;
$month = $t;
} else {
$daynum = $numdays-($tt-1);
$month = $avg_num_month-($t-1);
}
$sequence = $key;
$nodaycount=true;
}
}
if (
$nodaycount==false)
$day++;
}
$u++;
}
}
?>

我今天注意到,在我們的 RounCalendar 中,這段程式碼在計算上需要修改,出現了零日的情況。這個函式將會正確計算曆法中的月份和日期。這段程式碼的功能相當廣泛,我很抱歉它沒有經過完善的測試,需要一些重構。
-2
simon at chronolabs dot org dot au
17 年前
這將會回傳馬雅長計曆,其週期在 2012 年達到 13.0.0.0.0。為了程式庫的緣故,我不得不壓縮 changemaya 函式,所以如果您想把它展開,歡迎您這麼做。它適用於 Unix 時間戳記。要呼叫這個例程,請使用以下語法。馬雅曆法是一種已有大約 5000 年歷史的日計數曆法,它追蹤我們在銀河系中的天體位置。

<?php

echo MayanLongCount(time());

function
MayanLongCount($tme){

$config = array('ppo' => array(13,0,0,0,0),
'epoch' => strtotime('2012-12-21'));

$diff=(($tme-$config['epoch'])/(60*60*24));
$ppo = changemaya($config['ppo'],ceil($diff));

return
$ppo[0].'.'.$ppo[1].'.'.$ppo[2].'.'.$ppo[3].'.'.$ppo[4];
}

function
changemaya($ppo,$diff){
if (
$diff>0) { $amount=1; } else { $amount=-1; }
for (
$sh=1;$sh<abs($diff);$sh++){ if ($ppo[4]+$amount>20){
if (
$ppo[3]+$amount>20){ if ($ppo[2]+$amount>20){
if (
$ppo[1]+$amount>20){ if ($ppo[0]+$amount>20){
$ppo[0]=0; $ppo[1]=0; $ppo[2]=0; $ppo[3]=0; $ppo[4]=0;
} else {
$ppo[1]=0; $ppo[0]=$ppo[0]+$amount;}
} else {
$ppo[2]=0; $ppo[1]=$ppo[1]+$amount; }
} else {
$ppo[3]=0; $ppo[2]=$ppo[2]+$amount; }
} else {
$ppo[4]=0; $ppo[3]=$ppo[3]+$amount; }
} elseif (
$ppo[4]+$amount<0){ if ($ppo[3]+$amount<0){
if (
$ppo[2]+$amount<0){ if ($ppo[1]+$amount<0){
if (
$ppo[0]+$amount<0){ $ppo[0]=20; $ppo[1]=0;
$ppo[2]=0; $ppo[3]=0; $ppo[4]=0;
} else {
$ppo[1]=20; $ppo[0]=$ppo[0]+$amount; }
} else {
$ppo[2]=20; $ppo[1]=$ppo[1]+$amount; }
} else {
$ppo[3]=20; $ppo[2]=$ppo[2]+$amount; }
} else {
$ppo[4]=20; $ppo[3]=$ppo[3]+$amount; }
} else {
$ppo[4]=$ppo[4]+$amount;}}
return
$ppo;
}

?>

謝謝!!
-2
curlee at mindspring dot com
21 年前
我解決了在 JD Edwards ERP 套件(運行於 AS/400 上)中使用的儒略日期的問題。此系統的儒略日期格式如下:CYYDDD

其中 C 代表世紀,0 表示 1900,1 表示 2000
DDD 是一年中的第幾天

我使用了 PHP 內建的 mktime 函式將日期轉換為標準的 DD/MM/YYYY 格式。此函式可以轉換 1970 年到 2038 年之間的日期(這是 Unix 時間戳記和 mktime 函式的限制)。

變數 $jde_date 必須是一個 6 位數的字串.... 如果您使用數值變數類型,它會捨棄代表 1900 年的任何日期的前導 0.... 這會破壞 substr 函式,從而導致整個過程出錯。

<?php
function jde_date_conv($jde_date)
{

$ct = substr($jde_date,0,1);
$yr = substr($jde_date,1,2);
$dy = substr($jde_date,3,3);

if(
$ct == 0) $yr_pfx = 19;
if(
$ct == 1) $yr_pfx = 20;

$tlt_yr = $yr_pfx.$yr;

$base_time = mktime(0,0,0,1,0,$tlt_yr);

$unix_time = ($dy * 86400) + $base_time;

return
date("m/d/Y" , $unix_time);
}
?>
To Top