Catch the week correctly

Asked

Viewed 71 times

2

Colleagues,

How would I get to pick up the week of a given month? For example: if next week the user accesses the system (today is day 18/11/16), would appear:

Current week: day 20/11/2016 to 27/11/2016

I accept suggestions in PHP, Jquery or Javascript.

Thank you

  • Would that be ? date('d-m-Y', strtotime("+1 week"));

3 answers

1

To add this system in PHP is simple.

date('d-m-Y', strtotime("+1 week"));

Using this code you will take the current date and add an extra week to it (+1 week).

Accessing today, the output will be

25-11-2016

Then to stay as you want, just do

echo date('d-m-Y')." à ". date('d-m-Y', strtotime("+1 week"));

18/11/2016 to 25/11/2016

Suggested reading: strtotime()

An alternative is to use Javascript.

var date = new Date();
date.setDate(date.getDate() + 7);

Add 7 more days from current date.

Suggested reading: Setdate();

  • Perfect Mauro, I only have a doubt. Let’s assume that the user accesses on 22/11/2016. How would it appear 20/11/2016 a 27/11/2016?

  • Then you would have to do a date check (22/11). But you could explain why, so that I can implement and pass you ?

  • for a report of the current week

  • is fixed, always on day 22 ?

1

You can use the Moment.js:

var primeiroDia = moment().startOf('isoWeek').toDate();
var ultimoDia = moment().endOf('isoWeek').toDate();

console.log(primeiroDia);
console.log(ultimoDia);

jsfiddle :)

0

Made in js Puro

  var dataHoje = new Date(); //Pegar a data atual
    var diaSemana = dataHoje.getDay(); //Pegar o dia da semana
    var diaSemana = 7 - dataHoje; //Quantidade de dias que faltam para terminar a semana
    var primeiroDia = new Date(dataHoje.setDate(dataHoje.getDate() + dias)); //Primeiro dia da semana que vem
    var ultimoDia = new Date(dataHoje.setDate(primeiroDia.getDate() + 7)); //Segundo dia da semana que vem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.