Programming, WordPress

Time ago implementation

WordPress has a function to determine the difference between two timestamps. Time ago can be implemented in the WordPress Post, using get_the_time() and human_time_diff() functions.

$postedTime = get_the_time(‘U’,$post->ID); //Returns Unix timestamp
get_the_time() function retrieves the time at which the post was written.

human_time_diff( int $from, int $to ), determines the difference between two timestamps by providing the posted date of the post and current time.

$timeAgo = human_time_diff($postedTime ,current_time( ‘U’ ));

Example:

Click to learn more about get_the_time() and human_time_diff() functions in codex.

Code to determine the difference between two timestamps in PHP:

Source: https://syframework.alwaysdata.net/human-time-diff

We can also use Carbon, a simple PHP API extension for DateTime to implement different Date and Time formats.

Hits: 41

Standard