0

I am trying to create a thing where I know when a post was created and then insert a time 4 hours into the future into my database...

I then have another page that lists all the posts out and shows a countdown to the expiration date...

I use the current time() and then compare it to the expiring time of the post in the database to come up with the amount of hours, minutes and seconds left in the countdown. But I am having trouble... i go on my computer and look at the countdown and then see a completely different number on my friends computer.

Is there something with the difference in server time? or what?

Basically this is what I have in my database

Post title-> "My Post Title" Time Expires-> 13:50:02

Then I have my display_post.php:

while ($allQuestions = mysql_fetch_array($r)){
   $now = new DateTime(date("G:i:s",time()));
   $exp = new DateTime($allQuestions['time_expires']);
   $diff = $now->diff($exp);
   printf('%d hours, %d minutes, %d seconds', $diff->h, $diff->i, $diff->s);
}

I have no clue as to how this is done... I also might have trouble in the insertion of a new post which is this code here:

$time_created = date("h:i:s",time());
$time_expires = date("h:i:s",time()+(4*3600));
$query = "INSERT into questions (time_created, time_expires)VALUES('".$time_created."', '".$time_expires."')";
mysql_query($query) or die(mysql_error());

So how would I go about making it so a user can insert a new post and then he/she (and others in DIFFERENT timezones) go to the page to see how much time is left until it expires (4 hours since creation)?

Also will being in a different timezone effect this number that is displayed in countdown?

2 Answers 2

2

your code is adding 4 seconds to the current time

$time_expires = date("h:i:s",time()+4);

if you need to add 4 hours then your code must be like this:

$time_expires = date("h:i:s",time()+14400);

Also, you need to set the timezone, don't you get a php warning for that ?

date_default_timezone_set("UTC"); // is referring to the GMT timing 

So do all your calculation based on UTC and everything should be fine.

EDIT: USE MYSQL to make your php code simple.

instead of using this:

$time_created = date("h:i:s",time());
$time_expires = date("h:i:s",time()+(4*3600));
$query = "INSERT into questions (time_created, time_expires)VALUES('".$time_created."', '".$time_expires."')";
mysql_query($query) or die(mysql_error());

Use this:

$query = "INSERT into questions (time_created, time_expires) 
    VALUES(CURTIME(),CURRENT_TIMESTAMP + INTERVAL '4' HOUR)"

mysql_query($query) or die(mysql_error());

Another thing is your connection to the database

mysql_connect This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

if you need to know more about PDO click HERE

2
  • Also, you can use SQL statements to calculate the time difference, instead of using php to do it.
    – Othman
    Mar 4, 2013 at 5:28
  • @cipherous look to the new query I just added
    – Othman
    Mar 4, 2013 at 5:35
2

The difference is due to timezones seen in most cases. Kindly try after setting the time zone correction and do things:

$timezone = "Asia/Calcutta";
if(function_exists('date_default_timezone_set'))
   date_default_timezone_set($timezone);

I am using the above code. To know your timezone kindly visit http://php.net/manual/en/timezones.php

Also found that your are not adding seconds in the code:

$time_expires = date("h:i:s",time() + 60*60*4);
2
  • Thanks for the quick response, I noticed the adding seconds thing when I uploaded this question.. But if I add the the date_default_timezone_set() will it work on everyones browser? and will it always use the timezones time?
    – Wp3Dev
    Mar 4, 2013 at 5:20
  • @cipherous As PHP is the server side programming and thus it works for all browsers. Timezones are used to set the time according to your time zone and yes it works always
    – Vineet1982
    Mar 4, 2013 at 5:22

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.