Jump to content

Day Counter


b0lero
Go to solution Solved by Ch0cu3r,

Recommended Posts

I really have no idea what you mean based on your description. Here's some code that "may" help you.

 

 

$numberOfDays = 20;
 
for($d=0; $d<$numberOfDays; $d++)
{
    $dayNo = $d+1;
    echo "Day {$dayNo}: " . date("M j, Y", strtotime("+{$d} days")) . "<br>\n";
}
Link to comment
Share on other sites

 

I really have no idea what you mean based on your description. Here's some code that "may" help you.

$numberOfDays = 20;
 
for($d=0; $d<$numberOfDays; $d++)
{
    $dayNo = $d+1;
    echo "Day {$dayNo}: " . date("M j, Y", strtotime("+{$d} days")) . "<br>\n";
}

Nope :P its not that one :) ill try to explain simple sorry about my english :P let say i press the botton start  today and from today every single day will be counted into my web page , so after 20 day's counter will show TODAY IS DAY 20 :)

Edited by b0lero
Link to comment
Share on other sites

So, each user gets to determine the start date? Is the date counter based on calendar days or 24 hour periods from when the user starts the counter? For example, if the user starts the counter at 10:00AM today, does it move to the next number each day at midnight or 10:00AM?

 

You will need to store the date of when the user clicks to start the counter. You can either do this with a cookie (which the user can reset) or you can do it in a database (which requires the user to log in to see the current counter value).

 

Once you have a saved value for the start of the counter, you just need to add some logic to count the different between the start of the counter and the current day. That will depend on where you store it and when the counter should be incremented (midnight or same time as originally set). If you store the start date/time in the database you would want to do the calculation there: DATEDIFF(). If you do it in a cookie, you will need to do the calculation with PHP: DateTime::diff

Link to comment
Share on other sites

So, each user gets to determine the start date? Is the date counter based on calendar days or 24 hour periods from when the user starts the counter? For example, if the user starts the counter at 10:00AM today, does it move to the next number each day at midnight or 10:00AM?

 

You will need to store the date of when the user clicks to start the counter. You can either do this with a cookie (which the user can reset) or you can do it in a database (which requires the user to log in to see the current counter value).

 

Once you have a saved value for the start of the counter, you just need to add some logic to count the different between the start of the counter and the current day. That will depend on where you store it and when the counter should be incremented (midnight or same time as originally set). If you store the start date/time in the database you would want to do the calculation there: DATEDIFF(). If you do it in a cookie, you will need to do the calculation with PHP: DateTime::diff

The users are only one it's for private using and the day should star when the user press the button start and then 24h period also i prefer to connect it to myqsl not cockies cos if i visit the page from different ip the counter will be again 0

Link to comment
Share on other sites

  • Solution

If this is for only your use, then a mysql database is a bit overkill. You store the date in a text file when the button is pressed. Example code

<?php 

// change date.txt to the filename for storoing the date in
define('DATE_FILE', 'date.txt');

// set to true if you want the counter to increment at midnight
define('COUNT_FROM_MIDNIGHT', false);

// when form is submitted
if(isset($_POST['start']))
{
    // save current time to file
    file_put_contents(DATE_FILE, date('Y-m-d h:i:s'));
}

// if the date file has been created
if(file_exists(DATE_FILE))
{
    // get todays date
    $today = new DateTime();

    // read the date recorded
    $recoredDate = file_get_contents(DATE_FILE);

    // set the date to compare
    $date = new DateTime($recoredDate);
    // if we are counting from midnight, set the hour, minute and seconds to zero
    if(COUNT_FROM_MIDNIGHT)
        $date->setTime(0, 0, 0);

    // calcualate the difference between the two dates
    $diff = $today->diff($date);
    // output the difference
    echo $diff->format('Today is day %a');
}
// file has not been created, show button to start counter
else
{
    $date = date('Y-m-d');
    $time = date('h:i:s');
    echo '
    <form method="post">
        <input type="submit" name="start" value="Start Counter" /> ' .
        'From ' . $date . ' ' . $time .
        '<br />Counter increments at ' . (COUNT_FROM_MIDNIGHT ? 'midnight' : $time . ' tomorrow') .
    '</form>';
}

?>

To simulate the difference in days, change  $today = new DateTime();  to a date in the future eg  $today = new DateTime('2015-06-30 09:21:50');

Link to comment
Share on other sites

Wow thanks so mutch mate thats fantastic , so  text document keeps all the data and i don't have to visit the web site from the same ip adress to see update day info :) briliant !

 

P.S  I got problem with my Like This button , when i press it browser send message ''You have reached your quota of positive votes for the day'' witch is not true :\

Link to comment
Share on other sites

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.