WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to convert a post's date/year from Gregorian calendar to Hebrew calendar? (8 posts)

  1. Copernicus
    Member
    Posted 5 years ago #

    For a project I'm working on, we wish to convert the date of posts within a single category from the Gregorian calendar year (2009, for example) to the Hebrew calendar year (5769, in this case).

    Does anyone know how to do this? I found the PHP snippet below, but have no idea how to call it within WordPress so that it would convert the date output with a post to read in the Hebrew year.

    Any & all help is appreciated!

    Sample code snippet that seems to do what we're looking for:

    <?php
    $gregorianMonth = date(n);
    $gregorianDay = date(j);
    $gregorianYear = date(Y);
    
    $jdDate = gregoriantojd($gregorianMonth,$gregorianDay,$gregorianYear);
    
    $hebrewMonthName = jdmonthname($jdDate,4);
    
    $hebrewDate = jdtojewish($jdDate);
    
    list($hebrewMonth, $hebrewDay, $hebrewYear) = split('/',$hebrewDate);
    
    echo "$hebrewDay $hebrewMonthName $hebrewYear";
    
    ?>
  2. MichaelH
    Member
    Posted 5 years ago #

    In a loop, these variables for your routine:

    $gregorianMonth = mysql2date("n", $post->post_date);
    $gregorianDay = mysql2date("j", $post->post_date);
    $gregorianYear = mysql2date("Y", $post->post_date);
  3. Copernicus
    Member
    Posted 5 years ago #

    Oh, man, Michael, thank you for the great start.

    I'm definitely getting closer, but can't get it to work just yet.

    So far, I have:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php
    $gregorianMonth = mysql2date("n", $post->post_date);
    $gregorianDay = mysql2date("j", $post->post_date);
    $gregorianYear = mysql2date("Y", $post->post_date);
    
    $jdDate = gregoriantojd($gregorianMonth,$gregorianDay,$gregorianYear);
    $hebrewMonthName = jdmonthname($jdDate,4);
    $hebrewDate = jdtojewish($jdDate);
    list($hebrewMonth, $hebrewDay, $hebrewYear) = split('/',$hebrewDate);
    ?>

    Then, in calling the posts, I am using either:

    <?php
    global $post;
    $myposts = get_posts('numberposts=99&amp;category=40');
    foreach($myposts as $post) :
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_time(Y); ?></a>
    <?php endforeach; ?>

    or

    <?php
    global $post;
    $myposts = get_posts('numberposts=99&amp;category=40');
    foreach($myposts as $post) :
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_time($hebrewYear); ?></a>
    <?php endforeach; ?>

    The first bit of code accurately displays the Gregorian year of each post (for example 2006 & 2009 for two sample posts). The second bit of code displays the Hebrew year - BUT it's only the current Hebrew year, not the Hebrew year of the post (both of them showing up as 5769 rather than 5767 and 5769).

    Does anything in the above set-up jump out as responsible for that problem?

    Thanks again for the help!

  4. MichaelH
    Member
    Posted 5 years ago #

    Just as a note, I didn't confirm that your code to create the Hebrew date works...

    You have three different post loops there but only show date conversion logic in the first loop.

    If that isn't the issue you may need to paste your complete template to a pastebin http://wordpress.pastebin.ca/ and report the link back here.

  5. Copernicus
    Member
    Posted 5 years ago #

    Thanks again, Michael, for all your help!
    It turns out, for whatever reason, I needed to have the code

    $gregorianMonth = mysql2date("n", $post->post_date);
    $gregorianDay = mysql2date("j", $post->post_date);
    $gregorianYear = mysql2date("Y", $post->post_date);
    $jdDate = gregoriantojd($gregorianMonth,$gregorianDay,$gregorianYear);
    $hebrewMonthName = jdmonthname($jdDate,4);
    $hebrewDate = jdtojewish($jdDate);
    list($hebrewMonth, $hebrewDay, $hebrewYear) = split('/',$hebrewDate);

    in BOTH loops.

    Adding the code to both has it now working!

    There's probably a more efficient way to do this, but at this point, I'm just happy that it works.

    Thank you!

  6. Copernicus
    Member
    Posted 5 years ago #

    Follow-up (posted for the benefit of anyone else that might seek a similar solution):

    I'm embarrassed to say that, after all of that work, the code snippet I had did not accurately transliterate the years (for example 12/15/2006 became 5766 instead of 5767).

    Thus, I found another way utilizing a combination of what I learned from MichaelH (above) and Rob Marsh's "Recent Posts" plugin.

    Now, I employ the following code:

    <?php if (function_exists('recent_posts')) recent_posts('included_cats=1&amp;limit=10&amp;output_template=<p><a href="{url}">{php:echo(jdtojewish(gregoriantojd({date:n,j,Y})));}</a></p>'); ?>

    and it displays the posts (in the sample above, the category of interest has the category ID of "1") with the proper Hebrew date as a link.

    Unfortunately, it displays the FULL date (MM/DD/YYYY) instead of just the year (which is what I require in my specific project)... but at least it works. As a note, the jdtojewish PHP function needs the full date in MM,DD,YYYY format to transliterate properly into a Hebrew year.

    If I had better chops, I could probably figure out how to use the above code AND make it only display the year. For now, I do not have those chops.

    Thanks again to MichaelH for the help.

  7. MichaelH
    Member
    Posted 5 years ago #

    At the very least couldn't you do something like:

    $datestring = jdtojewish(gregoriantojd({date:n,j,Y}))
    echo substr($datestring, -4);
  8. Copernicus
    Member
    Posted 5 years ago #

    That worked perfectly! Thank you MichaelH.
    The only thing I had to change to get it working was the addition of a ";" before the "echo."

    So, now, the entire snippet reads:

    <p>{php:$datestring=jdtojewish(gregoriantojd({date:n,j,Y})); echo substr($datestring, -4);}</p>

    Thanks again, this was a huge help!

Topic Closed

This topic has been closed to new replies.

About this Topic