Convert Gregorian dates to solar dates in WordPress and WooCommerce
P粉662361740
P粉662361740 2023-09-03 16:27:14
0
1
546
<p>I have designed a template for my website and I would like all of the <strong>WordPress</strong> and <strong>WooCommerce</strong> dates in the template to be converted from the Gregorian calendar to the Gregorian calendar. </p> <p>I use the following <strong>shortcode</strong> to display the date my posts were published: </p> <pre class="brush:php;toolbar:false;"><?php the_time('Y/m/d'); ?></pre> <p>But the date it is showing is in Gregorian calendar and I want to change it to Shamsi. </p> <p>I know there are many plugins to achieve this, but I want to add some code to my template so that my default template will always be a sun date. </p> <p>I need any help using this feature. </p> <p>Thank you very much. </p> <hr /> <p><strong>Editor:</strong></p> <p>I have tried all these codes before but none of them changed the date in WordPress for me. </p> <p><strong>I am looking for a code that will convert all WordPress dates or at least display them by putting it in the <strong><code>functions.php</code></strong> The date is in my solar template. </strong></p> <p>HijriCalendar.class.php</p> <p>Persian Calendar</p> <hr /> <p><strong>Edit 2: </strong></p> <p>I used moment.js library in the template and also used the <strong>javascript</strong> method but still it doesn't work. </p> <p>How to change Gregorian date to Persian date in JavaScript? </p> <p>Convert Gregorian date to Persian (Jalali) date in Angular 2 and Ionic 2</p>
P粉662361740
P粉662361740

reply all(1)
P粉752812853

I think first you use get_the_time function to get the date and then convert it to shamsi date and echo it for display. Code like below works. You can add any other g2p functionality you want.

add_filter('the_time', 'change_date_format');

function change_date_format(){
    //change date language here
    $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))){
        /* 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 >= 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);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!