首页 > 解决方案 > 如何在 WordPress 中获取 Timber/Twig 格式的周数和天数?

问题描述

所以我在 WordPress 中设置了 2 个 Timber/Twig 变量datenowdateborn我想用它来计算宠物的周龄和天数,如下所示:您的宠物现在:6 周零 3 天!我想知道是否有人愿意在这里为我写这篇文章——我将非常感激!!

仅供参考感谢:Twig 日期差异打印正确的天数,即。对我来说是46 天,但现在需要将其转换为6 周零 3 天

{% set difference = date(datenow).diff(date(dateborn)) %}
  {% set leftDays = difference.days %}
  {% if leftDays == 1 %}
    1 day
  {% else %}
    {{ leftDays }}
    days
  {% endif %}

标签: wordpresstwigadvanced-custom-fieldstimber

解决方案


尽量保持你的视图文件尽可能清晰——里面没有逻辑。让我们创建过滤器

function getDogYears( $dateborn ) {

    // today
    $today = gmdate( 'Y-m-d' );

    // difference between today and passed birth date in seconds
    $timeDiff = strtotime( $today ) - strtotime( $dateborn );

    // If it is below zero (future - per wasn't born yet) return some kind of placeholder for it 
    if ( $timeDiff < 0 ) {
        return '0 days';
    }

    // Here we will collect data
    $birthData = [];

    /**
     * We've got difference in seconds let's count it in years by dividing it on special constant YEAR_IN_SECONDS - it is basically the result of 365 * 24 * 60 * 60.
     * 'floor()' function will round result (eg 5.6 = 5 full years)
     */
    $years = floor( $timeDiff / YEAR_IN_SECONDS );

    // if it is non-aero value pass it
    if ( $years ) {

        // `_n()` will handle singular and plural form for us - if $years === 1 it will return '1 year', otherwise 'n years'
        $birthData[] = sprintf( _n( '%s year', '%s years', $years ), $years );
    }

    // same for months but we need exclude years from remaining time difference
    $months = floor( ( $timeDiff - $years * YEAR_IN_SECONDS ) / MONTH_IN_SECONDS );
    if ( $months ) {
        $birthData[] = sprintf( _n( '%s month', '%s months', $months ), $months );
    }
    
    // etc...
    $weeks = floor( ( $timeDiff - $years * YEAR_IN_SECONDS - $months * MONTH_IN_SECONDS ) / WEEK_IN_SECONDS );
    if ( $weeks ) {
        $birthData[] = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
    }

    $days = floor( ( $timeDiff - $years * YEAR_IN_SECONDS - $months * MONTH_IN_SECONDS - $weeks * WEEK_IN_SECONDS ) / DAY_IN_SECONDS );
    if ( $days ) {
        $birthData[] = sprintf( _n( '%s day', '%s days', $days ), $days );
    }

    // `implode()` will convert array of data to a string with ', ' separator 
    $dogYears = implode( ', ', $birthData ); // will return 'N years, X months, Y weeks, Z days' as a string

    // return - filters MUST return something
    return $dogYears;
}

// add `getDogYears()` function to a Timber filter as `dogyears`
add_filter( 'timber/twig', 'add_to_twig' );
function add_to_twig( $twig ) {
    // for version 2+
    // $twig->addFilter( new \Twig\TwigFilter( 'dogyears', 'getDogYears' ) );
    $twig->addFilter( new \Timber\Twig_Filter( 'dogyears', 'getDogYears' ) );
    return $twig;
}

像这样使用它

// .twig

Your pet is now {{ post.meta('datebotn')|dogyears }} old

例如today = 07.07.2021

Your pet is now {{ '05.07.2021'|dogyears }} old = Your pet is now 2 days old
Your pet is now {{ '07.02.2018'|dogyears }} old = Your pet is now 3 years, 5 months, 1 day old 
Your pet is now {{ '25.02.2014'|dogyears }} old = Your pet is now 7 years, 4 months, 2 weeks old 

如果没有必要,您可以考虑从过滤器中返回完整的短语,或者改为创建函数 - 这取决于您


推荐阅读