0

I am trying to create a "time since" post display so when someone views a post they will see "posted about a week ago" instead of the date.

I've tried a few things but to no avail and I think it is because I found out that my default timezone is GMT (using date_default_timezone_get()) and my date.timezone (using ini_get('date.timezone')) is America/Denver. My actual timezone is America/New York (or EDT).

I need to find the current logged in user's timezone (not sure how to do this - maybe a setting in their profile), and then show the time since the post was published. I am sure I need to take the time stamp from the DB (which I believe is all UTC) use strtotime and then figure out what the current logged in user's timezone is and create the "time since" message.

I do have a method that calculates the time since a post but it has incorrect times based on the timezone info I found out.

Here's the code I am using:

public function time_since($stored_time){

$time_elapsed = time() - strtotime($stored_time);

$tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
);

foreach ($tokens as $unit => $text) {
    if ($time_elapsed < $unit) continue;

    $numberOfUnits = floor($time_elapsed / $unit);

    return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}

}//end time_since

If anyone can push me in the right direction I would greatly appreciate it.

1 Answer 1

0

You don't need to know the user's timezone, because time since the post was published is independent of timezone.

WordPress sets your timezone to UTC so that internally, all dates are dealt with the same and consistent timezone. You should avoid using php functions, and instead use the WordPress provided functions.

Incidently, human_time_diff() does exactly what you're after..

To get the time since a post have been published...

//Unix timestamp of post
$gmt_timestamp = get_post_time( 'G', true, $post );

//Echo time since timestamp in human readable form
printf( __( '%s ago' ), human_time_diff( $gmt_timestamp ) );

Edit

human_time_diff() only goes up to days unfortunately. But here's one that estimates weeks, months, and years too (though obviously not exactly as there aren't a fixed number of days in a month). It's untested.

 wpse_human_time_diff($from, $to=''){

      if ( empty($to) )
           $to = time();
      $diff = (int) abs($to - $from);

      if ( $diff <= 3600 ){
            $mins = round($diff / 60);
            if ($mins <= 1) {
                  $mins = 1;
            }

            /* translators: min=minute */
            $since = sprintf(_n('%s min', '%s mins', $mins), $mins);

       }elseif( $diff <= 86400 ){
            $hours = round($diff / 3600);
            $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);

       }elseif( $diff <= 604800 ) {
            $days = round($diff / 86400);
            $since = sprintf(_n('%s day', '%s days', $days), $days);

       }elseif( $diff <= 2592000 ){
            $weeks = round($diff / 604800);
            $since = sprintf(_n('%s week', '%s weeks', $weeks), $weeks);

       }elseif( $diff <= 31536000 ){
            $months = round($diff / 2592000);
            $since = sprintf(_n('%s month', '%s months', $months), $months);

       }else{
            $years = round($diff / 31536000)
            $since = sprintf(_n('%s year', '%s years', $years), $years);
       }

       return $since;

}
2
  • Using this gave me "15575 days ago" on a post submitted yesterday. I failed to mention that the date is stored in another table in the DB as a MYSQL timestamp field. It is for users to be able to post small messages - but not posts. Would this have an affect on the time since? Maybe I defined something incorrectly?
    – dkmojo
    Commented Aug 22, 2012 at 13:53
  • Actually nix my last message. I added strtotime to my time stamp from the DB and it worked perfectly! Seems to do everything correctly now. Thanks for the tip and for pointing out the human_time_diff() method. :)
    – dkmojo
    Commented Aug 22, 2012 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.