Jump to content

using the date function to find last week start and end.


Recommended Posts

Hi,

 

I need to find the start of last week (monday) and the end of last week (sunday) using the date function in a Y-m-d format.

 

But it needs to work this out regardless of todays date.

 

Finding a day in the past is not a problem if you know how many days to minus from the date function, but how can I work this to be not dependent on this fact?

 

Thanks. Eon201

If i understand what you want, this should do:

 

<?php
echo 'Last Monday: '.date('Y-m-d',strtotime('last monday',time()-60*60*24*6)).'<br />';
echo 'Last Sunday: '.date('Y-m-d',strtotime('next sunday',time()-60*60*24*);
?>

 

We use the strtotime function to find the two dates. You may wonder why it says next sunday. That is because we set the current time for the strtotime as a week ago. Otherwise, last sunday is one day before last monday. Which i dont think is what you want. You'll then we wondering why we actually only go back 6 days for last monday, or 8 days for next sunday. That is because we need to be careful of the situation where it is monday or sunday today. If it is monday today, and we go back 7 days, then last monday is in fact two weeks ago. Similarly, if today is sunday, and we go back a week, next sunday is in fact today.

That goes back one exactly one week.

 

What im looking for is this...

 

Lets say today is Friday the 30th Nov.

 

And we want to find the beginning of last week (Mon 19th), and the end of it (Sun 25th).

 

We could do that by just minusing from the timestamp.

 

But. My script will run every day, therefore the inital day will change. When it changes we would then need to minus a differetn amount from the timestamp.

 

Does that make sense. This is quite tricky to explain!

 

 

In a nutshell. Whatever the date/day of the week. I need to find the start (mon) and end (sun) of the previous week.

 

Thanks so far. Eon201

 

 

That goes back one exactly one week.

 

What im looking for is this...

 

Lets say today is Friday the 30th Nov.

 

And we want to find the beginning of last week (Mon 19th), and the end of it (Sun 25th).

 

We could do that by just minusing from the timestamp.

 

But. My script will run every day, therefore the inital day will change. When it changes we would then need to minus a differetn amount from the timestamp.

 

I think you'll find thats what my script does:

 

Last Monday: 2007-11-19
Last Sunday: 2007-11-25

 

If you wanted to test it further, then add some time just after the call to the time() function:

<?php
$offset = 60*60*24*7;//pretend 'now' is one week in the future
echo 'Last Monday: '.date('Y-m-d',strtotime('last monday',time()+$offset-60*60*24*6)).'<br />';
echo 'Last Sunday: '.date('Y-m-d',strtotime('next sunday',time()+$offset-60*60*24*);
?>

 

Produces:

 

Last Monday: 2007-11-26
Last Sunday: 2007-12-02

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.