"Years Since" Shortcode
Adds a [years_since] shortcode that prints how many full years have passed since a date, such as years in business, years of experience or a person's age, so the number never goes out of date.
Opens WPIDE on
Code
add_action( 'init', function () {
add_shortcode( 'years_since', function ( $atts ) {
$atts = shortcode_atts( array( 'date' => '' ), $atts, 'years_since' );
$start = DateTimeImmutable::createFromFormat( '!Y-m-d', $atts['date'], wp_timezone() );
if ( ! $start ) {
return '';
}
return esc_html( number_format_i18n( $start->diff( new DateTimeImmutable( 'now', wp_timezone() ) )->y ) );
} );
} );
Notes
Example "Serving customers for [years_since date='2009-05-01'] years".
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.