cobusbo Posted April 1, 2016 Share Posted April 1, 2016 (edited) Hi I'm trying to use my countdown timer to do a certain action when time is reach in this example I want to point users to another page, but seems like I'm going wrong somewhere along the way. When it reaches 0 it just starts counting up from 0 <html> <?php define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $targetDate = new DateTime('2016-09-29 23:00:00'); $remaining = $targetDate->diff(new DateTime()); // not done if( $remaining > 0 ) { echo $remaining->format('%a D - %H:%I:%S'); //--> 86 D - 19:44:24 } // done else { header("Location: testlogin.php"); exit(); } ?> </html> Edited April 1, 2016 by cobusbo Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/ Share on other sites More sharing options...
ginerjm Posted April 1, 2016 Share Posted April 1, 2016 Maybe I'm confused, but how does this ever get to be zero? Your target is 5 months away. Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/#findComment-1532703 Share on other sites More sharing options...
cobusbo Posted April 1, 2016 Author Share Posted April 1, 2016 Maybe I'm confused, but how does this ever get to be zero? Your target is 5 months away. That's just an example date I've tested it with current date and time Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/#findComment-1532705 Share on other sites More sharing options...
ginerjm Posted April 1, 2016 Share Posted April 1, 2016 So wouldn't that have been a good example to show us? Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/#findComment-1532706 Share on other sites More sharing options...
cobusbo Posted April 1, 2016 Author Share Posted April 1, 2016 So wouldn't that have been a good example to show us? As requested I've tried something new with time() <html> <?php define('TIMEZONE', 'Africa/Harare'); date_default_timezone_set(TIMEZONE); $targetDate = new DateTime('2016-04-01 20:59:00'); $remaining = $targetDate->diff(new DateTime()); $now = time(); // not done if( $now > $targetDate ) { echo $remaining->format('%a D - %H:%I:%S'); //--> 86 D - 19:44:24 } // done else { header("Location: testlogin.php"); exit(); } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/#findComment-1532707 Share on other sites More sharing options...
ginerjm Posted April 1, 2016 Share Posted April 1, 2016 Once again the failure to include simple PHP error checking during a development costs you time and frustration. You are comparing a datetime var against an integer and that's what I get when I run your script: Notice: Object of class DateInterval could not be converted to int in /home/albany/public_html/homejg/test.php on line 10 See my signature. Quote Link to comment https://forums.phpfreaks.com/topic/301135-countdown-timer-actions-not-working/#findComment-1532708 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.