Jump to content

Php only countdown with action to follow when countdown time reach 0


Recommended Posts

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";
?>

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 by BuildMyWeb

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.

 

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...

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 by Barand

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";

 

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>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.