-1

I'd like to add a counter to my website which counts in days. However I'd also like to add a button where this can be reset back to 0.

After searching I found the below code, and all is needed now is the button for it. I'm no Javascript expert so any help would be great.

I used this code to create the counter:

<script type="text/javascript">
//Set the two dates
var startdate=new Date(2013, 11, 16) //Month is 0-11 in JavaScript
today=new Date()
//Get 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
document.write(Math.ceil((today.getTime()-startdate.getTime())/(one_day))+
" days since your last drink!")
</script>

Is there a way I can include a button to reset the start date to the current date (for example if pressed today it would change from 16th of December 2013 to the 19th of December 2013)

Does anyone have any idea how I would do this? I'm fairly new to javascript so just learning the ropes.

Thanks

Gary

  • 1
    Did you Google the subject? What have you tried so far? What did you ran into? – Bas van Dijk Dec 19 '13 at 10:13
  • Hi, I've been googling all morning and the only thing i've found is counting between two dates (Which I'm sure I could do and believe this would be the way to create the first part), it's the reset function I'm unsure of and how to reset the first date to the current date – user3118688 Dec 19 '13 at 10:17
  • You could add a cookie if one does not exist using php [setcookie](http://php.net/setcookie), which can also be used to 'reset' the cookie on your button click. Then on page load you can retrieve this cookie using [$_COOKIE](http://www.php.net/manual/en/reserved.variables.cookies.php). Using the current date and the cookies date you can then calculate the [date different](http://stackoverflow.com/questions/1940338/date-difference-in-php-on-days) to display. – Jonathan Dec 19 '13 at 10:18

1 Answers1

0

@Jonathan has given a good method of achieving this using cookies.This is what most of the sites use to save your preferences.

Another possible way is that you can make use of database to save the startDate for each user and update it accordingly when the reset button is set.The next time you fetch startDate it will be the updated date.You can save it where you are saving his profile information.

If you have small number of users you can also use xml file to store the startDate information and updating it accordingly.

I would go for database or cookies.

Hope it helps

Naveen
  • 7,944
  • 12
  • 78
  • 165
  • Hi, I'm unsure if I've explained correctly. The counter will be reset by the user. For example, it will count how many days they go without chocolate, and should they fall off the wagon, they press reset and start counting again. Would this work with the cookie option? I understand that each user would have to have their own and at the moment a database isn't an option. Thanks again for responding! – user3118688 Dec 19 '13 at 12:19