dbach
dbach

Reputation: 163

PHP server side incremental counter

sorry I am new to PHP and need some help/guidance on creating a counter that will work server side, so I guess update an initial value?

I need for example to start with a base number of 1500 and have that number increase by 1 every 2 minutes, obviously so any visitors will see an increased number each time the visit.

Would the initial value need to be stored in sql or can a txt file be updated?

Any help would be great,

Thanks

Upvotes: 2

Views: 1008

Answers (4)

user2742371
user2742371

Reputation:

It can be done in SQL if you want it but a text file is OK too, just save a value (1500), then create a cronjob and let it execute a PHP file where you'll have to set up the code that executes an SQL query which updates that value OR the code to update that text file every 2 minutes.

Example:

# Every two minutes
*/2 * * * * /your/path/too/this/file/updatecode.php

In your PHP file:

$SQL = "UPDATE table SET columnname = columname + 1";
// etc...

// OR the text file update code

Upvotes: 1

Joeme
Joeme

Reputation: 513

If you don't need to store it specifically for some reason, then you don't need to run cron etc... Take a time stamp of a specific point in time you want to start at. Then calculate minutes since and add it to your start number (1500)

//Start Number
$n = 1500;
$cur_time = time();
$orig_time = strtotime("2013-10-21 10:00:00");

//New Number + difference in minutes (120 seconds for 2 mins) since start time
$newn = $n + round(abs($cur_time - $orig_time) / 120,0);

// Output New Number
echo $newn;

And if you wanted it in one line for copy/paste

 echo 1500 + round(abs(time() - strtotime("2013-10-21 10:00:00")) / 120,0);

Upvotes: 0

inquam
inquam

Reputation: 12932

If storing it is needed a SQL database for this is probably overkill. Create you number, serialize it and store it to a file. Load it from the file next time, unserialize, increment, serialize and save. You can save a time stamp along with the number to the file to avoid having to run some job every 2 minutes and instead calculate the correct value when you load the number back from the file.

Something like this (but error checking etc should be added and I haven't actually tried it to make sure the calculation is correct... but the main idea should be visible).

<?php

if(file_exists('mydatafile')) {
  $data = unserialize(file_get_contents('mydatafile'));
  // Calculate correct value based on time stamp
  $data['number'] += round((time() - $data['timestamp']) / 120);
}
else {
  // Init the number
  $data["number"] = 1500;
}

// Print it if you need to here

// Update time stamp
$data["timestamp"] = time();

// Serialize and save data
file_put_contents('mydatafile', serialize($data)));

Upvotes: 0

Darren Crabb
Darren Crabb

Reputation: 580

You could do this without a database just using dates. work out the difference in time between two dates (the current date and the starting date when you created the script), then divide that down into the correct amount of milliseconds for 2 minutes, and add that to your initial 1500.

Upvotes: 0

Related Questions