Skip to content

Instantly share code, notes, and snippets.

@ThatGuySam
Last active October 24, 2017 18:11
Show Gist options
  • Save ThatGuySam/233b5ba488d74e8cba9787d252da0c5f to your computer and use it in GitHub Desktop.
Save ThatGuySam/233b5ba488d74e8cba9787d252da0c5f to your computer and use it in GitHub Desktop.
Time since Shortcode for Wordpress
<?php
// ex:
// With [time since="1997"] years experience
// outputs: With 20 years of experience
class SCCTimeShortcode {
static $add_script;
static function init() {
add_shortcode('time', array(__CLASS__, 'handle_shortcode'));
}
static function handle_shortcode($atts) {
extract( shortcode_atts( array(
'since' => "",
'in' => 'y'
), $atts, 'time' ) );
$since_timestamp = strtotime($since);
// Then
$then = new DateTime(date( 'Y-m-d', $since_timestamp ));
// Now
$now = new DateTime(date( 'Y-m-d' ));
$diff = $now->diff( $then );
// https://gist.github.com/Victa/3523765
// Get the difference in the unit specified(years by default)
$output = $diff->{$in};
return $output;
}
}
SCCTimeShortcode::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment