Human Time Diff function (posted x days ago)
-
Hi,
Before I start, here’s my blog: https://www.ayconatest.co.nf. It has a normal theme and a mobile theme (You can see it using https://www.mobilephoneemulator.com/).
Here’s my problem: I have this bit of code in my mobile theme that displays post time in a relative way: “posted x minutes/days ago”. Here it is (in the functions.php file):
if ( ! function_exists( 'wps_time_since_post' ) ) : /** * Calculate the time since the post was published (minutes, hours, days) * * If the post was made more than a week ago then the date is simply displayed * */ function wps_time_since_post( $long_form = true, $post_timestamp ) { $now_timestamp = time(); $minute_in_seconds = 60; $hour_in_seconds = 3600; $day_in_seconds = 86400; $week_in_seconds = 604800; $offset = abs( $now_timestamp - $post_timestamp ); if( $offset <= $minute_in_seconds ) { $span = 'just now'; } elseif( $offset < $hour_in_seconds) { $span = round( $offset / $minute_in_seconds ) . ' minutes ago'; } elseif( $offset < $day_in_seconds ) { $span = round( $offset / $hour_in_seconds ) .' hours ago'; } elseif( $offset < $week_in_seconds ) { $span = round( $offset / $day_in_seconds ) .' days ago'; } else { if( $long_form ) $span = date( "F j, Y g:ia T" , $post_timestamp ); else $span = date( "F j, Y" , $post_timestamp ); } return $span; } endif; if ( ! function_exists( 'wps_posted_on' ) ) : /** * Meta text of author name and text publish date * */ function wps_posted_on( $long_form = true ) { $display = "none"; if( wps_get_option( 'show_post_author' ) == true ) $display = "block"; return "<span style=\"display:$display\">Posted " . wps_time_since_post( $long_form, get_post_time() ) . " by " . get_the_author() . "</span>"; } endif;
And it’s called in the single.php file like this:
<?php echo wps_posted_on(); ?>
It works fine on the mobile theme. But when I tried using it in the normal theme, it didn’t work.
My non-mobile theme initially uses this function:
if ( ! function_exists( 'imbalance2_posted_on' ) ) : function imbalance2_posted_on() { printf( __( '%1$s', 'imbalance2' ), sprintf( '<span class="entry-date">%1$s</span>', get_the_date() ) ); } endif;
I substituted it with the one in the mobile theme & then called it in the loop-single.php and loop.php so instead of <?php imbalance2_posted_on() ?> I put <?php echo wps_posted_on(); ?>, but it gives me:
Call to undefined function wps_get_option() in C:\wamp\www\wordpress\wp-content\themes\imbalance2\functions.php on line 402
402 in functions.php is the line:
if( wps_get_option( 'show_post_author' ) == true )
I don’t know PHP very much, so any input would be very appreciated.
Thanks.
- The topic ‘Human Time Diff function (posted x days ago)’ is closed to new replies.