简体   繁体   中英

php/html display automatic week counter starting from a specific data

Is there any coding (php / HTML or JS script) to display a week counter and start this counter from a specific date say i wanted to start this counter on Monday 25th March 2019 (and just display "Week 1"), then the number increases every week or 7 days (so on Monday 1st April 2019 it would change to "Week 2") until it gets to 52 weeks? Any help would be grateful!!!! thanks!

This can be done with plain JavaScript using the Date object.

First initialize a new Date from which you want to start counting

var startDate=new Date(2019,2,10); // will set the date to the 10th of March

Second get the actual time and date

var today=new Date();

Now simply get the difference between this two dates like this:

var difference=today-startDate;

This will return the difference in milliseconds.

To get the difference in days proceed like this

var days = difference / 1000 / 60 / 60 / 24;

Based on this you can get the number of weeks that passed by

var weeks = parseInt(days / 7);

Here's a working example:

 var startDate = new Date(2019, 2, 10); var today = new Date(); document.getElementById("stDate").innerHTML = "target: <b>" + startDate.toDateString() + "</b>"; document.getElementById("cDate").innerHTML = "today: <b>" + today.toDateString() + "</b>"; var difference = today - startDate; var days = difference / 1000 / 60 / 60 / 24; var weeks = parseInt(days / 7); var messaged = ""; if (weeks < 1) { message = "no weeks have passed"; } else { message = "weeks passed: <b>" + weeks + "</b>"; } document.getElementById("result").innerHTML = message;
 <div id="stDate"></div><br> <div id="cDate"></div><br> <div id="result"></div><br>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM