将公历日期转换为阳历在WordPress中

huangapple go评论64阅读模式
英文:

Convert Gregorian date to solar in WordPress

问题

我已经在 functions.php 文件中包含了一段代码片段,用于将公历日期转换为太阳历。

我使用以下的 shortcode 来显示我的帖子的发布日期,它可以正确地从公历转换为太阳历:

<?php the_time('Y/m/d'); ?>

我还使用以下的 shortcode 来显示我的帖子更新日期:

<?php the_modified_time('Y/m/d'); ?>

这个 shortcode 的日期转换代码无法正常工作,仍然显示公历日期。

是否有可能编辑这段代码,使得这个 shortcode 也支持更新日期并将其转换为太阳历日期?

我放在 functions.php 文件中的代码如下:

add_filter('the_time', 'change_date_format');

function change_date_format(){
    // 在此更改日期语言
    $date = get_the_time('Y/m/d');
    $date = explode('/', $date);
    $farsi_date = g2p($date[0],$date[1],$date[2]);
    return $farsi_date[0].'/'.$farsi_date[1].'/'.$farsi_date[2];
}

function g2p($g_y, $g_m, $g_d)
{
    $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);

    $gy = $g_y-1600;
    $gm = $g_m-1;
    $gd = $g_d-1;

    $g_day_no = 365*$gy+floor(($gy+3)/4)-floor(($gy+99)/100)+floor(($gy+399)/400);

    for ($i=0; $i < $gm; ++$i){
        $g_day_no += $g_days_in_month[$i];
    }

    if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0))){
        /* 闰年且在二月后 */
        ++$g_day_no;
    }

    $g_day_no += $gd;
    $j_day_no = $g_day_no-79;
    $j_np = floor($j_day_no/12053);
    $j_day_no %= 12053;
    $jy = 979+33*$j_np+4*floor($j_day_no/1461);
    $j_day_no %= 1461;

    if ($j_day_no >= 366) {
        $jy += floor(($j_day_no-1)/365);
        $j_day_no = ($j_day_no-1)%365;
    }
    $j_all_days = $j_day_no+1;

    for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i) {
        $j_day_no -= $j_days_in_month[$i];
    }

    $jm = $i+1;
    $jd = $j_day_no+1;

    return array($jy, $jm, $jd, $j_all_days);
}
英文:

I have included a code snippet in the functions.php file that converts the Gregorian date to solar.

I use the following shortcode to display the publication date of my posts, which correctly converts from Gregorian to Solar:

&lt;?php the_time(&#39;Y/m/d&#39;); ?&gt; 

I also use the following shortcode to display my posts updates:

&lt;?php the_modified_time(&#39;Y/m/d&#39;); ?&gt;

The date conversion code for this shortcode doesn't work and still shows me Gregorian date.

Is it possible to edit this code so that the shortcode also supports updating the date and converts it to solar date?

The code I put in the functions.php file:

add_filter(&#39;the_time&#39;, &#39;change_date_format&#39;);
function change_date_format(){
//change date language here
$date = get_the_time(&#39;Y/m/d&#39;);
$date = explode(&#39;/&#39;, $date);
$farsi_date = g2p($date[0],$date[1],$date[2]);
return $farsi_date[0].&#39;/&#39;.$farsi_date[1].&#39;/&#39;.$farsi_date[2];
}
function g2p($g_y, $g_m, $g_d)
{
$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
$gy = $g_y-1600;
$gm = $g_m-1;
$gd = $g_d-1;
$g_day_no = 365*$gy+floor(($gy+3)/4)-floor(($gy+99)/100)+floor(($gy+399)/400);
for ($i=0; $i &lt; $gm; ++$i){
$g_day_no += $g_days_in_month[$i];
}
if ($gm&gt;1 &amp;&amp; (($gy%4==0 &amp;&amp; $gy%100!=0) || ($gy%400==0))){
/* leap and after Feb */
++$g_day_no;
}
$g_day_no += $gd;
$j_day_no = $g_day_no-79;
$j_np = floor($j_day_no/12053);
$j_day_no %= 12053;
$jy = 979+33*$j_np+4*floor($j_day_no/1461);
$j_day_no %= 1461;
if ($j_day_no &gt;= 366) {
$jy += floor(($j_day_no-1)/365);
$j_day_no = ($j_day_no-1)%365;
}
$j_all_days = $j_day_no+1;
for ($i = 0; $i &lt; 11 &amp;&amp; $j_day_no &gt;= $j_days_in_month[$i]; ++$i) {
$j_day_no -= $j_days_in_month[$i];
}
$jm = $i+1;
$jd = $j_day_no+1;
return array($jy, $jm, $jd, $j_all_days);
}

答案1

得分: 1

添加另一个与您已经使用的相同过滤器函数的过滤器

add_filter('the_modified_time', 'change_date_format');
英文:

Hook another filter with the same filter function you already use.

add_filter(&#39;the_modified_time&#39;, &#39;change_date_format&#39;); 

huangapple
  • 本文由 发表于 2023年6月26日 01:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定