How do implement this time ago php code into Woking with my WordPress site’s posts

define( TIMEBEFORE_NOW,         'now' );
define( TIMEBEFORE_MINUTE,      '{num} minute ago' );
define( TIMEBEFORE_MINUTES,     '{num} minutes ago' );
define( TIMEBEFORE_HOUR,        '{num} hour ago' );
define( TIMEBEFORE_HOURS,       '{num} hours ago' );
define( TIMEBEFORE_YESTERDAY,   'yesterday' );
define( TIMEBEFORE_FORMAT,      '%e %b' );
define( TIMEBEFORE_FORMAT_YEAR, '%e %b, %Y' );

function time_before( $time )
{
    $out    = '';
    $now    = time();
    $diff   = $now - $time;

    if( $diff < 60 )
        return TIMEBEFORE_NOW;

    elseif( $diff < 3600 )
        return str_replace( '{num}', ( $out = round( $diff / 60 ) ), $out == 1 ? TIMEBEFORE_MINUTE : TIMEBEFORE_MINUTES );

    elseif( $diff < 3600 * 24 )
        return str_replace( '{num}', ( $out = round( $diff / 3600 ) ), $out == 1 ? TIMEBEFORE_HOUR : TIMEBEFORE_HOURS );

    elseif( $diff < 3600 * 24 * 2 )
        return TIMEBEFORE_YESTERDAY;

    else
        return strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time );
}

//CSS to show time on cursor hover

                .box time
                {
                    font-weight: 700;
                    cursor: help;
                }

A Better Way To Present Time On the Web

    Wait for  60 seconds for real-time date increments.


<section class="box">
    Published <time data-time="<?=( $time = time() )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

<section class="box">
    Published <time data-time="<?=( $time = strtotime( '-1 minute' ) )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

<section class="box">
    Published <time data-time="<?=( $time = strtotime( '-3 hours' ) )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

<section class="box">
    Published <time data-time="<?=( $time = strtotime( '-25 hours' ) )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

<section class="box">
    Published <time data-time="<?=( $time = strtotime( '-2 days' ) )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

<section class="box">
    Published <time data-time="<?=( $time = strtotime( '-1 year -2 days' ) )?>" datetime="<?=date( 'Y-m-d', $time )?>" title="<?=strftime( date( 'Y', $time ) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, $time )?>"><?=time_before( $time )?></time>
</section>

//script src=”jquery-v1.min.js” //script

//script

'use strict';

;( function( $, window, document, undefined )
{
    var $elements       = $( 'time[data-time]' ),
        strNow          = '<?=TIMEBEFORE_NOW?>',
        strMinute       = '<?=TIMEBEFORE_MINUTE?>',
        strMinutes      = '<?=TIMEBEFORE_MINUTES?>',
        strHour         = '<?=TIMEBEFORE_HOUR?>',
        strHours        = '<?=TIMEBEFORE_HOURS?>',
        strYesterday    = '<?=TIMEBEFORE_YESTERDAY?>',
        updateDates     = function()
        {
            $elements.each( function()
            {
                var $this   = $( this ),
                    time    = $this.attr( 'data-time' ),
                    now     = Math.round( new Date().getTime() / 1000 ),
                    diff    = now - time,
                    out     = '';

                if( diff < 60 )
                    out = strNow;

                else if( diff < 3600 )
                    out = ( ( out = Math.round( diff / 60 ) ) == 1 ? strMinute : strMinutes ).replace( '{num}', out );

                else if( diff < 3600 * 24 )
                    out = ( ( out = Math.round( diff / 3660 ) ) == 1 ? strHour : strHours ).replace( '{num}', out );

                else if( diff < 3600 * 24 * 2 )
                    out = strYesterday;

                else
                    out = $this.attr( 'title' );

                $this.text( out );
            });
            setTimeout( updateDates, 1000 * 60 );
        };

    setTimeout( updateDates, 1000 * 60 );

})( jQuery, window, document );

//script

‘,
strMinute = ”,
strMinutes = ”,
strHour = ”,
strHours = ”,
strYesterday = ”,
updateDates = function()
{
Array.prototype.forEach.call( elements, function( entry )
{
var time = entry.getAttribute( ‘data-time’ ),
now = Math.round( new Date().getTime() / 1000 ),
diff = now – time,
out = ”;

if( diff

//script

'use strict';

;( function( $, window, document, undefined )
{
    var $element      = $( '.the-counter' ),
        $boxes        = $( '.box' ).slice( 0, 2 ),
        count         = 60,
        updateCounter = function()
        {
            count--;
            if( count < 0 )
            {
                count = 60;
                $boxes.addClass( 'is-highlight' );
                setTimeout( function(){ $boxes.removeClass( 'is-highlight' ); }, 3000 );
            }
            $element.text( count );
            setTimeout( updateCounter, 1000 );
        };

    updateCounter();

})( jQuery, window, document );

//script

Read more here: How do implement this time ago php code into Woking with my WordPress site’s posts

Leave a Reply

Your email address will not be published. Required fields are marked *