So I have a 2 Timber/Twig variables set to datenow
and dateborn
in WordPress that I want use to calculate a pets age in weeks and days like this: Your pet is now: 6 weeks and 3 days old! I wonder if anyone is up to writing this out for me on here - I would be tremendously grateful!!
FYI Thanks to: Twig date difference prints the correct number of days ie. 46 days for me but need to convert it now to 6 Weeks and 3 Days
{% set difference = date(datenow).diff(date(dateborn)) %}
{% set leftDays = difference.days %}
{% if leftDays == 1 %}
1 day
{% else %}
{{ leftDays }}
days
{% endif %}
Try to keep your view files as clear as possible - no logic inside. Let's create filter
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;
}
Use it like
// .twig
Your pet is now {{ post.meta('datebotn')|dogyears }} old
For example 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
You may consider to return full phrase from your filter if it is unnecessary or create function instead - it is up to you