PHP Get and add timer

Hello. I would like to know how to add some time to server side timer. I wanna make a script that it checks if people has thing if he has it Then run timer every 5 mins. At the end of the timer do something. I know how to play with ifs. The problem is that I dont know how to create a timer, its eather timmer with cookies or server side.
as I know by now.
$timestamp = time();
$datum = date(“Y-m-d H:i:s”,$timestamp);

Gives you current server time. And I need to make a loop that does somehing in php every 5 mins.
I think its easyer to do it on javascript, but not really a fan of it. -_-
So Im asking you guys to help me!

You could use a cron job for that, set to run the script every five mins

Im not really familiar with corn, and still I preff php scripts because
I will still need a timer:
that countdowns till 0 (Once). for exemple if a guy clicks some nav thing and accepts bla bla, it starts count down to 0 for exemple 10 mins, and when it gets completed it does something, but the problem is I know how to make a countdown, but I dont know how to make a coundown with cookies, or server side, so when the guy would refresh it wouldnt refresh the count.

I think you’re probably better off doing this in JavaScript. From the story you’re suggesting it looks like you assume that the user stays at your site while the countdown is running.
You can use jQuery to set the start time in a session cookie and count down from that. Perhaps also use jQuery to send an ajax request to your site at the start moment which you can store in your DB and to which you can validate the time passing after 10 minutes should something special happen.

Do you know how to do that? Cuz I do really need help, I know its less chances in php, and more easy on JS, Im more advenced on php then js though. So My knowelage is minimum on js, I just know how to do some dialogs with options and stuff. And a question, a jQuery countdown timer will be running aslong as the user is on the website, what if the guy would close the website would that kill the countdown?

Since PHP is only a hypertext preprocessor, it’ll execute once before page load and no more. It’s therefore impossible to do this in PHP alone. If you’re wanting this task to execute every five minutes for certain people only and want to see realtime results, then a cron job won’t not be a viable solution either. That pretty much leaves you with JavaScript.

What you’ll want to do is setup a JS timer for those people who need the task executed every five minutes (this could be done via a simple IF statement on the PHP side to include the JS code or not). When five minutes is reached, setup an Ajax call to a PHP script to send an asynchronous web request to the server to execute your task.

Here’s two resources to help you achieve this:
JavaScript Timing Events
Making Ajax Calls

Ill deffinetly have a look! Thank you :slight_smile:

so. I have this so far.

I’ve created a small countdown Timer using PHP, jQuery, and Ajax, I am sure it could be modified to do what you want it to do:

Here’s the main file PHP and HTML

date_default_timezone_set('America/Detroit'); // Set the Default Time Zone:

if (isset($_POST['action']) && $_POST['action'] == 'enter') {
	$futureDate = date('Y-m-d H:i:s',strtotime($_POST['futureDate']));
	$_SESSION['future'] = $futureDate;

<meta charset="utf-8">
<title>The Count Down Clock</title>
<link rel="stylesheet" href="css/style.css">


<?php echo (isset($futureDate)) ? '<h1 class="container headingDate">' . $futureDate . '</h1>' : '<h1 class="container headingDate">2015-07-04</h1>'; ?>
<form id="countDownForm" class="container rounded shadow" action="countDownClock.php" method="post">
  <input type="hidden" name="action" value="enter">
  <label for="countDownStyle" class="cmsLabel">Enter Future Date: </label>
  <input id="countDownStyle" name="futureDate" value="" type="datetime" placeholder="0000-00-00 00:00:00" required>
  <input type="submit" name="submit" value="Submit" class="submitBtn">

<div class="container clockBox rounded shadow">
  <p class="clock"></p>
<script src="//"></script>
<script src="web.countdown.ajax.02.js"></script>

Here the jQuery (JavaScript)

$(function() {
	/*  The Countdown Timer to call the Ajax Method */
	var updateTime = setInterval(displayTime, 1000);		
	/* The Ajax Method of Getting Time */
	function displayTime() {
		var $clock = $('.clock');
			$.ajax({ // Start of ajax:
				url: 'sendCountDown.02.php',   // Pulling time from the server:         
				dataType: "json", // Format type:
				success: function(info) { // Grab the data from php and then display it:
					// Variables * Self-Explanatory *
					var days       =  info.countDown.days, // Grab total days till expiration:
							hours      =  info.countDown.h, // Grab total hours till expiration:
					    minutes    =  info.countDown.i, // Grab total mins till expiration:
							seconds    =  info.countDown.s, // Grab total secs till expiration:
							$msg       =  ''; 
							if (hours < 10) {
								hours = '0' + hours;
							if (minutes < 10) {
								minutes = '0' + minutes;
							if (seconds < 10) {
								seconds = '0' + seconds;	
							$msg =  days + ' Days '  + hours + ' Hours ' + 				
							 minutes + ' Minutes ' + seconds + ' Seconds';
							/* Display Time in Message */				 
						error: function (response) {
							var r = jQuery.parseJSON(response.responseText);
								alert("Message: " + r.Message);
								alert("StackTrace: " + r.StackTrace);
								alert("ExceptionType: " + r.ExceptionType);
						}); // End of ajax call:
	} // End of Function:	

And here’s the php file that grabs the Date/Time

date_default_timezone_set('America/Detroit'); // Set the Default Time Zone:


$future = (isset($_SESSION['future'])) ?  $_SESSION['future'] :  '2015-07-04 00:00:00';

$expired = new DateTime($future);
$now = new DateTime();

$e['countDown'] = $now->diff($expired, true);

print json_encode($e); // JSON 

I’m pretty sure you could do something to the code, the code is far from being optimized for I was just basically goofing around with it. :smile:

Thanks, so I this timer uses server time as I understood, is there I way I would set minutes not the date, for exemple

$future = (isset($_SESSION[‘future’])) ? $_SESSION[‘future’] : ‘2015-07-04 00:00:00’;

So the time wouldnt be a ‘2015-07-04 00:00:00’ but for exemple.

$daten = new DateTime();
$daten->modify(“+30 minutes”);

$future = (isset($_SESSION[‘future’])) ? $_SESSION[‘future’] : $daten;

I tryed tis but it doesnt add 30 to the time, it adds eather more or shows all 0.

I mean, it would get current time from server. then add 30 mins to it, and then use it as future time, so it would count down only 30 mins - now is 2014 10 10 00:00:00 and it would count down to 2014 10 10 00:30:00
so it would go down to 00 from 30 mins.

And btw thank for the timer. this is really better then cookies one.