How to display a countdown using PHP and MySQL

How to get a countdown using PHP?

I want to show something like 3Days 4Hours. I have a date field coming from a MySQL table and you want to calculate it with today's date. At the moment, I only have the date, not the time stored in the database, but in the end I will do it. So at that time I could show 3Days as 4 hours 10 minutes 43 seconds.

This is what I tried but getting the wrong answer:

$datetime1 = new DateTime($starton);//$starton - date stored in db
$datetime2 = new DateTime(date());
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d days);

I am confused if this works based on server time or the zone where the user is coming from. Please guide me. When I have a time field, I think I might need jQuery to show live seconds, and therefore also minutes.

+5
source share
6 answers

IMHO, when you think about comparing time, you automatically start talking about Unix time. In fact, Unix time is the number of seconds that always increases. When you have 2 Unix timestamps, you can use simple arithmetic to understand the difference, and then translate the difference into a human-readable form.

Please note that Unix timestamps are common in almost all programming languages, so you can choose to do this in PHP, MySQL or JavaScript, and everything can work out the same way. I will show you the version of PHP.

So you want to do this:

  • Find Unix Timestamp for Target Event
  • Save it in a MySQL database
  • Unix
  • ,
  • /, .

PHP:

//Unix timestamp to Dec. 21, 2012 (midnight)
$unix_time = mktime(0,0,0,12,21,2012); 
//Connect to MySQL
//"INSERT INTO table (target_timestamp) VALUES ($unix_time);"


//----- user goes to page -----
//Connect to MySQL
$sql = "SELECT target_timestamp FROM table WHERE foo = bar";
$unix_time = do_query($sql);//do_query not defined, assume it gets the timestamp
$current_time = time();
$diff = $unix_time - $current_time
//$diff should be positive and not 0
if( 1 > $diff ){
   exit('Target Event Already Passed (or is passing this very instant)');
} else {
   $w = $diff / 86400 / 7;
   $d = $diff / 86400 % 7;
   $h = $diff / 3600 % 24;
   $m = $diff / 60 % 60; 
   $s = $diff % 60;

   return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}
+3

PHP , , script date_default_timezone_set(). :

MySQL- . . TimeDiff(): http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff. TimeFormat(), .

PHP. unix epoch → . - DateTime, . :

$datetime1 = new DateTime($starton);
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d days);

:

$datetime1 = date_create($starton);
$datetime2 = date();
$interval = date_diff($datetime1,$datetime2);
echo $interval->format('%d days');

, ; , , Javascript .

+1

jQuery, JavaScript, .

jQuery : http://keith-wood.name/countdown.html

db.

+1

100% , :

$start = strtotime($dbTime);
$now = time();
$differenceInSeconds = $start - $now;

, .

0

, , date() , . date_default_timezone_set([TIMEZONE]);, , .

, , , javascript jQuery , .

javascript date1.getTime() - date2.getTime(), , javascript ( )

, , JS.

0

This is the solution faced by some frameworks (e.g. Ruby on Rails), PHP is not one of them, I think that what you are looking for is something like:

http://www.phpro.org/examples/Convert-Seconds-To-Hours-Minutes-Seconds-Words.html

0
source

All Articles