How to handle dates, trying to calculate time since a post

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;

}

deneme bonusu veren sitelerbahis casinomakrobetceltabettipobet365pinbahispolobet