Php counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Php counter

Post 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);
    }
?>
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Php counter

Post by VladSun »

Files?
No... :)

Just define a start date, calculate the days difference between it and the current date and multiply by 10 ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Php counter

Post by aceconcepts »

$dat+10?

Not entirely clear how you want this to work. Will this script be called once a day?
djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Re: Php counter

Post by djcritch »

yea ok but can you give me a code example im a bit lost with it!

Thanks a lot!
djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Re: Php counter

Post by djcritch »

yes just needs to be called once a day say at middnight
djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Re: Php counter

Post by djcritch »

would something like this work?

Code: Select all

 
<?php
    if date => 23/10/2008;
        +count 10
    else
        +count 0
?>
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Php counter

Post by aceconcepts »

Have you thought about using a cron job for this?

Or are you already using one?
djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Re: Php counter

Post by djcritch »

no how would i go about doing that?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Php counter

Post 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.
djcritch
Forum Newbie
Posts: 11
Joined: Thu Mar 20, 2008 5:55 am

Re: Php counter

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Php counter

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Php counter

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Php counter

Post by aceconcepts »

So, you could use this method to run a script at anytime? Because djcritch needs the script to execute at midnight everyday.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Php counter

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Php counter

Post by aceconcepts »

Oh ok - that makews sense :D
Post Reply