Last active
March 5, 2025 14:53
-
-
Save ThatGuySam/233b5ba488d74e8cba9787d252da0c5f to your computer and use it in GitHub Desktop.
Time since Shortcode for Wordpress
This file contains hidden or 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