Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to find the monday of the current week with a PHP script.
I could do something like..
if day = tues, date equals date - 1... but what if the date is the first? then the monday date would be date = -1. I would have to count back from 31 or 30 or 29 depending on which month it is. Is there an easier way of doing all of this?Thanks!
Brett

It's a one-liner if you like unreadable code :-)
print "Last monday was ".date("m/d/Y", time() - (((date("w") + 5)%6) * 24 * 60 * 60));
The long and more readable version:
$day_of_week = date("w");//0 for sun, 6 for sat
$days_since_monday = ($day_of_week + 5)%6;
$seconds_since_monday = $dates_since_monday * 24 * 60 * 60;
$now_in_seconds = time();
$monday_in_seconds = $now_in_seconds - $seconds_since_monday;
$monday_in_words = date("m/d/Y", $monday_in_seconds);
print "Last monday was ".$monday_in_words;Good luck,
-SN

Ha ha...Thanks but I screwed it up.
You asked for the monday of the current week, but I gave you the code for the most recent (past) monday. The code is a little simpler if you want the monday of the current week:
print "Monday this week was or will be on ".date("m/d/Y", time() - ((date("w") - 1) * 24 * 60 * 60));

Hi SN,
I tried both of these lines of code, and they both currently give me the date 02/06/2006. This is what I was asking for in the first post. But, I have one more question :).How would I get the monday of next week?
Today is 2/9/06, so I would want 2/13/06..doesn't matter what format its in.Thanks,
Brett

Hi again SN,
The second line of code you posted actually works better. The first line only works when the current date is past the date asking for.
When I do:
$day[1] = date("m/d/Y", time() - ((date("w") - 1) * 24 * 60 * 60));
$day[2] = date("m/d/Y", time() - ((date("w") - 2) * 24 * 60 * 60));
$day[3] = date("m/d/Y", time() - ((date("w") - 3) * 24 * 60 * 60));
$day[4] = date("m/d/Y", time() - ((date("w") - 4) * 24 * 60 * 60));
$day[5] = date("m/d/Y", time() - ((date("w") - 5) * 24 * 60 * 60));echo '
';for($i = 1; $i <= 10; $i += 1)
{
echo $day[$i];
echo '
';
}They give the correct dates for this week.:
02/06/2006
02/07/2006
02/08/2006
02/09/2006
02/10/2006
I would like the exact same thing for the next (upcoming week).Thanks,
Brett

Yes they both give you 2/06/2006 today, but they'll give you different things on a Sunday. Last Sunday (2/5/06), the first version would give you 1/29/2006, whereas the second version would have given you 2/06/2006.
To get the Monday of next week (which can be different from next Monday), you just adjust the formula a bit:
time() + ((8 - date("w")) * 24 * 60 * 60);So, if today is sunday, then the code I gave in post 3 will give you tomorrow, and the code above will give you a week from tomorrow. If today is Monday, the post 3 code will give you today, and the code above will give you a week from today. If today is tuesday, the post 3 code will give you yesterday's date, and the code above will give you the upcoming monday's date.
Good luck,
-SN

Whoops I didn't see your most recent post. I see you figured out the problem with the first version.
-SN

![]() |
Problem with batch file
|
Batch file to rename file
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |