Mutley Posted April 10, 2007 Share Posted April 10, 2007 How would I do a countdown in minutes to the next whole hour? Like 23minutes until 1pm. Quote Link to comment https://forums.phpfreaks.com/topic/46405-php-countdown/ Share on other sites More sharing options...
wildteen88 Posted April 10, 2007 Share Posted April 10, 2007 Simple code: <?php // get the current hour (24 hour clock) and increase by 1 $n_hour = date('H')+1; // Here we work out the hour, in 12 hour clock format // we also set the am / pm of the hour if($n_hour > 23) { $n_hour = 12; $m = 'am'; } elseif($n_hour > 11) { $n_hour = date('g', mktime($n_hour)); $m = 'pm'; } else { $m = 'am'; } $c_mins = date('j'); $l_mins = 60 - $c_mins; $time_left = 'There are ' . $l_mins . 'minutes left until ' . $n_hour . $m; echo $time_left; ?> Quote Link to comment https://forums.phpfreaks.com/topic/46405-php-countdown/#findComment-225681 Share on other sites More sharing options...
Mutley Posted April 10, 2007 Author Share Posted April 10, 2007 How do I set this for GMT/London times? Quote Link to comment https://forums.phpfreaks.com/topic/46405-php-countdown/#findComment-225685 Share on other sites More sharing options...
Mutley Posted April 10, 2007 Author Share Posted April 10, 2007 Fixed it using: putenv("TZ=Europe/London"); However the minutes are wrong, it says 50min to 12, when there are only 20. Quote Link to comment https://forums.phpfreaks.com/topic/46405-php-countdown/#findComment-225690 Share on other sites More sharing options...
wildteen88 Posted April 10, 2007 Share Posted April 10, 2007 Add the following line to the top of script: date_default_timezone_set('Europe/London'); <?php date_default_timezone_set('Europe/London'); // get the current hour (24 hour clock) and increase by 1 $n_hour = date('H')+1; // Here we work out the hour, in 12 hour clock format // we also set the am / pm of the hour if($n_hour > 23) { $n_hour = date('g', mktime($n_hour)); $m = 'am'; } elseif($n_hour > 11) { $n_hour = date('g', mktime($n_hour)); $m = 'pm'; } else { $m = 'am'; } // get the current minutes $c_mins = date('i'); // take the current minutes from 60 (60 minutes in an hour) $l_mins = 60 - $c_mins; // Now we set up the message to be shown. $time_left = 'There are ' . $l_mins . 'minutes left until ' . $n_hour . $m; // echo the message we setup above echo $time_left; ?> Fixed it using: putenv("TZ=Europe/London"); However the minutes are wrong, it says 50min to 12, when there are only 20. I typed j instead i for the the date function when getting the current minutes. That is corrected now. Quote Link to comment https://forums.phpfreaks.com/topic/46405-php-countdown/#findComment-225691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.