如何将伊斯兰日历添加到公共日历?

问题描述 投票:0回答:1
php time calendar
1个回答
0
投票

要在公历旁边添加伊斯兰历,您需要实现两个日历之间的转换,并除了公历日期之外还显示伊斯兰日期。伊斯兰历是阴历,其日期是根据月相确定的。您可以通过以下方式修改代码来实现此目的:

  1. 添加将公历日期转换为伊斯兰日期的函数。

  2. 更新您的代码以在表格单元格中显示公历和伊斯兰日期。

这是修改后的代码:

<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Monday</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>

<?php

date_default_timezone_set('Asia/Jakarta');

function gregorianToIslamic($year, $month, $day) {
    // Implement the conversion logic here
    // Return the Islamic year, month, and day
    // Example: return [$islamicYear, $islamicMonth, $islamicDay];
}

$days_count = date('t');

$current_day = date('d');
$current_month = date('m');
$current_year = date('Y');

// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, $current_month, 1, $current_year);

$week_day_first = date('N', $time_first_day_of_month);

// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));

$next_dates = array();

for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
  echo '<tr>';
  $counter = 0;
    for ($d = $w; $d <= $w + 6; $d++){
      echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
      if($d > 0){
        // next month's dates
        if($d > $days_count){
          for($in = 1; $in <= 1; $in++){
            echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
          }
        }
        // today
        else if($current_day == $d){
          echo '<div class="current-day" style="font-weight: bold;color:red;"><span class="given-date">'.$d.'</span></div>';
        }
        // this month's dates
        else{
          echo '<span class="given-date">'.$d.'</span>';
        }

        // Display Islamic date (Assuming the function returns [$islamicYear, $islamicMonth, $islamicDay])
        list($islamicYear, $islamicMonth, $islamicDay) = gregorianToIslamic($current_year, $current_month, $d);
        echo '<br>Islamic: ' . $islamicYear . '-' . $islamicMonth . '-' . $islamicDay;
      }
      // last month's dates
      else{
        //Here comes previous dates
        $offset = $d - 1;
        echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
      }
      echo '</td>';
      $counter++;
    }
  echo '</tr>';
}
?>
</table>

请注意,应实现

gregorianToIslamic
函数,以使用适当的转换算法将公历日期转换为伊斯兰日期。伊斯兰历法由于对月相的依赖而很复杂,因此转换逻辑可能涉及各种计算。您可能需要使用库或预定义函数来准确处理伊斯兰日历转换。

© www.soinside.com 2019 - 2024. All rights reserved.