-
-
Save niladam/e11a4d920471e24610276b3977f00827 to your computer and use it in GitHub Desktop.
Time since Shortcode for Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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