I contacted the hosting team of my shared host at Godaddy to change the server time because I use different timezone (Cairo), but they couldn't change it on the shared hosting.
I hope you help me to modify the code I use to calculate the time since the user inserted the data to the Mysql database. I use datetime for date fields.
Here is the code:
//This is the date the code inserts to the database:
$date = new DateTime("now", new DateTimeZone('Africa/Cairo'));
$date=$date->format("Y-m-d H:i:s");
//And this is the php function that calculates the time since post published:
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => ' year',
2592000 => ' month',
604800 => ' week',
86400 => ' day',
3600 => ' hour',
60 => ' minute',
1 => ' second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'':'');
}
}
1 Answer 1
Instead of using your real timezone 'Africa/Cairo', you could use the timezone of the server (to match the database). Since you're looking for the time difference, it would be accurate.
Sign up to request clarification or add additional context in comments.
3 Comments
DevManX
The problem is that, I also use the date details inside the articles (eg. 1 May 2015 12:12:18), so I need both, the time since data inserted and the accurate datetime the user published his article.
olkid
Question: what is the reference point for "accurate datetime the user published his article"? Is that according to the timezone of the publisher or to Cairo?
DevManX
The reference is the Cairo time.
default