cobusbo Posted September 29, 2015 Share Posted September 29, 2015 Hi I'm trying to create a countdown timer strictly php only and do an action once the time runs out I found an example of a countdown timer but I'm uncertain on how to use it to show in the format D H:m:s ( 01 Days 23:01:20) and how to chaeck if the time is done and do the action and afterwards remove the countdown timer from showing on the page <?php // Define your target date here $targetYear = 2007; $targetMonth = 9; $targetDay = 10; $targetHour = 12; $targetMinute= 00; $targetSecond= 00; // End target date definition // Define date format $dateFormat = "Y-m-d H:i:s"; $targetDate = mktime($targetHour,$targetMinute,$targetSecond,$targetMonth,$targetDay,$targetYear); $actualDate = time(); $secondsDiff = $targetDate - $actualDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds"; ?> Quote Link to comment Share on other sites More sharing options...
BuildMyWeb Posted September 29, 2015 Share Posted September 29, 2015 (edited) php is server-side. meaning it will not update the time display on your page unless you refresh the page. if you want a dynamic timer that ticks down in your browser, you will need to use a client-side solution like javascript. i suck at js so not sure about this but you might be able to trigger your php script via AJAX and have the page update on time intervals rather than user refresh. <?php // Define your target date here $targetYear = 2007; $targetMonth = 9; $targetDay = 10; $targetHour = 12; $targetMinute= 00; $targetSecond= 00; // End target date definition // Define date format $dateFormat = "Y-m-d H:i:s"; $targetDate = mktime($targetHour,$targetMinute,$targetSecond,$targetMonth,$targetDay,$targetYear); $actualDate = time(); $secondsDiff = $targetDate - $actualDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); // not done if( $secondsDiff > 0 ) { echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds"; } // done else { } ?> Edited September 29, 2015 by BuildMyWeb Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted September 29, 2015 Share Posted September 29, 2015 Yes, your only chance is with AjAX if you want to make it strictly PHP. Another solution is to make the timer with JS and have the JS script active an AJAX function that would active the PHP script once the timer has passed. Quote Link to comment Share on other sites More sharing options...
cobusbo Posted September 29, 2015 Author Share Posted September 29, 2015 php is server-side. meaning it will not update the time display on your page unless you refresh the page. if you want a dynamic timer that ticks down in your browser, you will need to use a client-side solution like javascript. i suck at js so not sure about this but you might be able to trigger your php script via AJAX and have the page update on time intervals rather than user refresh. <?php // Define your target date here $targetYear = 2007; $targetMonth = 9; $targetDay = 10; $targetHour = 12; $targetMinute= 00; $targetSecond= 00; // End target date definition // Define date format $dateFormat = "Y-m-d H:i:s"; $targetDate = mktime($targetHour,$targetMinute,$targetSecond,$targetMonth,$targetDay,$targetYear); $actualDate = time(); $secondsDiff = $targetDate - $actualDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); // not done if( $secondsDiff > 0 ) { echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds"; } // done else { } ?> The User refresh is fine but how to output it as example 01 D - 23:01:10 format since the above code only provide 1 days 23 hours 10minutes 15 seconds etc... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 29, 2015 Share Posted September 29, 2015 (edited) Why don't you use DateTime object like I showed you in your other post? http://forums.phpfreaks.com/topic/298299-string-to-array-and-date/?do=findComment&comment=1521469 It saves you a shedload of code $targetDate = new DateTime('2015-12-25 12:00:00'); $remaining = $targetDate->diff(new DateTime()); echo $remaining->format('%a D - %H:%I:%S'); //--> 86 D - 19:44:24 Edited September 29, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
Barand Posted September 29, 2015 Share Posted September 29, 2015 If you want to use the current code (some DateTime stuff requires PHP5.3) then just change this line echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds"; Quote Link to comment Share on other sites More sharing options...
cobusbo Posted September 30, 2015 Author Share Posted September 30, 2015 Why don't you use DateTime object like I showed you in your other post? http://forums.phpfreaks.com/topic/298299-string-to-array-and-date/?do=findComment&comment=1521469 It saves you a shedload of code $targetDate = new DateTime('2015-12-25 12:00:00'); $remaining = $targetDate->diff(new DateTime()); echo $remaining->format('%a D - %H:%I:%S'); //--> 86 D - 19:44:24 Thank you Barand I tried your code and its Displaying the time in the correct format, but When the counter reaches 0 it starts to count up again and doesn't do my action in the if function <html> <? define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $targetDate = new DateTime('2015-09-29 20:00:00'); $remaining = $targetDate->diff(new DateTime()); // not done if( $remaining > 0 ) { echo '<span style="color:red"><b> We need to add some changes and need to disable chat to do so. Hopefully we will be back within an hour or so. Chat will go offline in: </b></span>'; echo $remaining->format('%a D - %H:%I:%S'); //--> 86 D - 19:44:24 } // done else { header("Location: offline.php"); exit(); } ?> </html> Quote Link to comment 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.