Skip to content

Instantly share code, notes, and snippets.

@Cheffheid
Created February 14, 2017 14:50
Show Gist options
  • Save Cheffheid/002b0a77d035b44d2cc3e0b7c30dc0e9 to your computer and use it in GitHub Desktop.
Save Cheffheid/002b0a77d035b44d2cc3e0b7c30dc0e9 to your computer and use it in GitHub Desktop.
Age shortcode for WordPress
function cheffism_age_function($atts) {
extract(shortcode_atts(array(
'date' => '12/31/1900', // DOB
'format' => 'dd/MM/YYYY' // Format, does not currently do anything
), $atts));
//explode the date to get month, day and year
$date = explode("/", $date);
//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $date[1], $date[0], $date[2]))) > date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
return $age;
}
add_shortcode('age', array( $this, 'cheffism_age_function') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment