2

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)?'':'');
 }
}
asked May 3, 2015 at 1:19

1 Answer 1

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.

answered May 3, 2015 at 1:29
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
The reference is the Cairo time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.