Skip to content

Instantly share code, notes, and snippets.

@alanef
Last active November 26, 2019 22:11
Show Gist options
  • Select an option

  • Save alanef/427146511eaebf3ec45c8ddbd5afd427 to your computer and use it in GitHub Desktop.

Select an option

Save alanef/427146511eaebf3ec45c8ddbd5afd427 to your computer and use it in GitHub Desktop.

Custom Plugin to show age today in a shortcode from date of birth

Download as a zip and install as a plugin

usage [age dob="12 july 1988"]

<?php
/*
Plugin Name: Age Shortcode
Plugin URI: https://fullworks.net
Description: Shortcode [age dob="28 feb 1971"] display age based on dob
Version: 0.1
Author: Alan
*/
add_shortcode( 'age', function ( $atts ) {
$a = shortcode_atts(
array(
'dob' => '28 feb 1971',
),
$atts );
$birthdate = new DateTime( date( "Y-m-d", strtotime( $a['dob'] ) ) );
$today = new DateTime( date( "Y-m-d" ) );
$age = $birthdate->diff( $today )->y;
echo $age;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment