eon201 Posted November 30, 2007 Share Posted November 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/ Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/#findComment-402923 Share on other sites More sharing options...
eon201 Posted November 30, 2007 Author Share Posted November 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/#findComment-402931 Share on other sites More sharing options...
eon201 Posted November 30, 2007 Author Share Posted November 30, 2007 Im going to be sending this info to the strtotime function, therefore should I use a dfifferent function to begin with. or is this the best way to do this?? Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/#findComment-402960 Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/#findComment-402992 Share on other sites More sharing options...
eon201 Posted November 30, 2007 Author Share Posted November 30, 2007 Sorry GingerRobot Thanks for the code. Will get busy with it now! Quote Link to comment https://forums.phpfreaks.com/topic/79557-using-the-date-function-to-find-last-week-start-and-end/#findComment-403013 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.