phpwordpresscommentspersian-calendar

how to convert get_comment_date() wordpress function to return persian date


In wordpress I use code below to get today persian's date.

function getDateIntl(?\DateTime $date = null, ?string $locale = null, ?DateTimeZone $timezone = null, $dateFormat)
{
    $date = $date ?? new \DateTime();
    $locale = $locale ?? \Locale::getDefault();
    $formatter = new \IntlDateFormatter(
        $locale,
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        $timezone,
        IntlDateFormatter::TRADITIONAL,
        $dateFormat
    );
    return $formatter->format($date);
}
echo getDateIntl(new DateTime(), "fa@calendar=persian", new \DateTimeZone('Asia/Tehran'), 'eeee، dd MMMM');

Now I'm in single blog posts and want to use this function to convert comments date to perisan defualt function of comment date is: get_comment_date()

defualt funtion of wordpress returns string that I can not replace it with "new DateTime()"


Solution

  • Wordpress has a special hook in which you can do any manipulation with the date when the function get_comment_date() will be used:

    add_filter( 'get_comment_date', 'my_get_comment_date_filter', 10, 3 );
    
    /**
     * Function for `get_comment_date` filter-hook.
     * 
     * @param string|int $date    Formatted date string or Unix timestamp.
     * @param string     $format  PHP date format.
     * @param WP_Comment $comment The comment object.
     *
     * @return string|int
     */
    function my_get_comment_date_filter( $date, $format, $comment ){
    
        // filter...
        return $date;
    }