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.