Page 1 of 2

Php counter

Posted: Thu Oct 23, 2008 9:34 am
by djcritch
Hi, i have this counter script but what i want it to do is increment by 10 each day i dont want to count my vistors i just want a script that will display a number and each day it adds 10 to that number!

am i anywhere near with this piece of code? if not can someone point me in the right direction!

Thanks!

Code: Select all

 
<?php
 
    if (file_exists('count_file.txt')) 
    {
        $fil = fopen('count_file.txt', r);
        $dat = fread($fil, filesize('count_file.txt')); 
        echo $dat+1;
        fclose($fil);
        $fil = fopen('count_file.txt', w);
        fwrite($fil, $dat+1);
    }
 
    else
    {
        $fil = fopen('count_file.txt', w);
        fwrite($fil, 1);
        echo '1';
        fclose($fil);
    }
?>
 

Re: Php counter

Posted: Thu Oct 23, 2008 9:37 am
by VladSun
Files?
No... :)

Just define a start date, calculate the days difference between it and the current date and multiply by 10 ;)

Re: Php counter

Posted: Thu Oct 23, 2008 9:38 am
by aceconcepts
$dat+10?

Not entirely clear how you want this to work. Will this script be called once a day?

Re: Php counter

Posted: Thu Oct 23, 2008 9:39 am
by djcritch
yea ok but can you give me a code example im a bit lost with it!

Thanks a lot!

Re: Php counter

Posted: Thu Oct 23, 2008 9:40 am
by djcritch
yes just needs to be called once a day say at middnight

Re: Php counter

Posted: Thu Oct 23, 2008 9:52 am
by djcritch
would something like this work?

Code: Select all

 
<?php
    if date => 23/10/2008;
        +count 10
    else
        +count 0
?>
 

Re: Php counter

Posted: Thu Oct 23, 2008 10:10 am
by aceconcepts
Have you thought about using a cron job for this?

Or are you already using one?

Re: Php counter

Posted: Thu Oct 23, 2008 10:46 am
by djcritch
no how would i go about doing that?

Re: Php counter

Posted: Thu Oct 23, 2008 11:14 am
by onion2k
A cron job is complete overkill for something like this. What Vladsun suggested, "Just define a start date, calculate the days difference between it and the current date and multiply by 10", is the right approach.

Re: Php counter

Posted: Thu Oct 23, 2008 11:23 am
by djcritch
yea i understand what i have to do now but can anyone one help me with the code im new to php

thanks for all your comments

Re: Php counter

Posted: Thu Oct 23, 2008 12:59 pm
by aceconcepts
onion2k wrote:A cron job is complete overkill for something like this. What Vladsun suggested, "Just define a start date, calculate the days difference between it and the current date and multiply by 10", is the right approach.
How would the script run though?

My understanding is that the script will not be manually invoked. Is this right djcritch?

Re: Php counter

Posted: Thu Oct 23, 2008 3:07 pm
by onion2k
aceconcepts wrote:How would the script run though?

Code: Select all

echo (((mktime(0,0,0,date("m"),date("d"),date("Y"),0)-mktime(0,0,0,3,28,1977,0))/86400)*10);
That echoes 10 times the number of days since I was born.

Re: Php counter

Posted: Thu Oct 23, 2008 3:52 pm
by aceconcepts
So, you could use this method to run a script at anytime? Because djcritch needs the script to execute at midnight everyday.

Re: Php counter

Posted: Thu Oct 23, 2008 5:00 pm
by onion2k
aceconcepts wrote:So, you could use this method to run a script at anytime? Because djcritch needs the script to execute at midnight everyday.
I don't think he does. I think that was him thinking of a way to increment the counter by 10 each day. He needs "a script that will display a number and each day it adds 10 to that number!" ... the code I posted does that.

Re: Php counter

Posted: Fri Oct 24, 2008 2:40 am
by aceconcepts
Oh ok - that makews sense :D