如何在 WordPress 中以 Timber/Twig 格式获取星期和日期?

How do I Get Weeks and days in Timber/Twig format in WordPress?

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

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

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

以下是我最终能够得出我想要的输出结果:当前年龄: 13 周零 5 天。 (dateborn = 4 月 1/21 datenow = 7 月 6/21)

但是由于我在开发方面很新,所以我仍然想知道是否会有 easier/cleaner 的写法,有人有什么想法吗?

  {% set difference = date(datenow).diff(date(dateborn)) %}
  {% set totaldays = difference.days %}
  {% set weekcalc = totaldays / 7 %}
  {% set weeksold = weekcalc | round(0, 'floor') %}
  {% set weeks2days = weeksold * 7 %}
  {% set daysold = totaldays - weeks2days %}
 
  <p>
      <strong>Current age:</strong>
      {{ weeksold }}
      Weeks and
      {{ daysold }}
      days old.
  </p>

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

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 

如果不需要,您可以考虑 return 来自过滤器的完整短语,或者改为创建函数 - 这取决于您