b0lero Posted June 24, 2015 Share Posted June 24, 2015 Hey gays im newbie in this and in php at all , what i need to do is day counter for each day... like today 1 ... tomorrow 2 ect.... how can i do it simple and not stresfull for the server as well thx Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/ Share on other sites More sharing options...
Psycho Posted June 24, 2015 Share Posted June 24, 2015 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514844 Share on other sites More sharing options...
b0lero Posted June 24, 2015 Author Share Posted June 24, 2015 (edited) 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 its not that one ill try to explain simple sorry about my english 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 June 24, 2015 by b0lero Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514846 Share on other sites More sharing options...
Psycho Posted June 24, 2015 Share Posted June 24, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514854 Share on other sites More sharing options...
b0lero Posted June 24, 2015 Author Share Posted June 24, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514856 Share on other sites More sharing options...
Solution Ch0cu3r Posted June 25, 2015 Solution Share Posted June 25, 2015 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'); Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514881 Share on other sites More sharing options...
b0lero Posted June 25, 2015 Author Share Posted June 25, 2015 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 :\ Quote Link to comment https://forums.phpfreaks.com/topic/297008-day-counter/#findComment-1514977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.