Skip to content

Instantly share code, notes, and snippets.

@niladam
Forked from ThatGuySam/time-since.php
Created October 7, 2017 08:01
Show Gist options
  • Save niladam/e11a4d920471e24610276b3977f00827 to your computer and use it in GitHub Desktop.
Save niladam/e11a4d920471e24610276b3977f00827 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