0

I have table of event which has a start_time and is saving a unix time. I want to show a countdown on my website based on server time so the countdown does not depend on users local time. I'm getting the start time and subtract it by current time on server :

$diff= $upc_tourn['start_t'] -time();

but I'm not sure how to show a countdown to my user based on this diff. I want the output be something like this:

2 days 10:25:30
Hirad Roshandel
  • 2,175
  • 5
  • 40
  • 63
  • 1
    I see your using php but below is a good answer for a JavaScript solution http://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer – Fintan Creaven Jul 12 '15 at 22:47
  • http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s for setting a time zone. With php you will have to communicate with the server to refresh countdown – Fintan Creaven Jul 12 '15 at 22:52
  • @FintanCreaven I dont want to change timezone – Hirad Roshandel Jul 12 '15 at 22:55
  • It all depends where the server is? Is sounds like you want a consistent time across all users this was a way of achieving it – Fintan Creaven Jul 12 '15 at 22:58
  • somewhat related thread (not a duplicate): [How do I set cookie expiration time in user local time?](http://stackoverflow.com/questions/4972861/how-do-i-set-cookie-expiration-time-in-user-local-time) – Nick Alexeev Jul 13 '15 at 07:19

1 Answers1

0

Ok below is what I came up with. It is a quick function you can simplify and extend to your liking.

And here is the FIDDLE

<?php

$diff= (time() + 172600) - time(); // replace (time() - 172600) with your time in the future.

function timedifftostr($timediff) {
  $days = floor($timediff / (60*60*24));
  $days_raw = $days * (60*60*24);
  $hours = floor(($timediff-$days_raw) / (60*60));
  $hours_raw = $hours * (60*60);
  $minutes = floor(($timediff-($hours_raw + $days_raw)) / (60));
  $minutes_raw = $minutes * (60);
  $seconds = floor($timediff-($hours_raw + $days_raw + $minutes_raw));
  $seconds_raw = $seconds;

  echo $days." Day/s ".str_pad($hours,2,0).":".str_pad($minutes,2,0).":".str_pad($seconds,2,0);
}
timedifftostr($diff);
?>

You could extend this further adding a check for multiple days and adding the s or not to day. The answer was provided long like to to show the working out of this for you in the future. Below I provide a quick breakdown the the maths.

  time() is in seconds.
  So time()/60 is time in minutes.
  So time()/60/60 is time in hours.
  So time()/60/60/24 is time in days.
n099y
  • 414
  • 2
  • 16
  • thanks but this is not a countdown it just shows the remaining time – Hirad Roshandel Jul 12 '15 at 23:15
  • @ Hirad Roshandel, No I realised that afterwards, the link provided in comments has a good example of one so I did not provide another. I can provide one if that link did not answer your question. – n099y Jul 12 '15 at 23:16