Dates - Last Sunday of the month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Dates - Last Sunday of the month

Post by t2birkey »

Looking for a way to find if today is the last Sunday of the month.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Dates - Last Sunday of the month

Post by Darhazer »

Code: Select all

 
$lastDay = date('t'); // number of days
$today = date('d');
$todayDay = date('D');
if ($todayDay != 'Sun' || ($lastDay - $today >=7) ) {
return false;
}
 
return true;
 
So, this code check if it Sunday and there are less than 7 days till the end of month, because if there is a 7th day, it will be also Sunday, and if it today is not Sunday, it obviously is not the last sunday
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: Dates - Last Sunday of the month

Post by t2birkey »

ty much, I will give this a try!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Dates - Last Sunday of the month

Post by McInfo »

This is the solution I came up with:

Code: Select all

$now = time();
if (date('l', $now) == 'Sunday' && date('n', $now) != date('n', strtotime('+1 week', $now))) {
    echo 'This is the last Sunday of the month.';
}
You might have problems with calling date() multiple times without having a fixed time. There was another topic where this caused a problem. I will have to look for it.

Edit: I found the topic: mysql_fetch_assoc not echoing date value

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:11 pm, edited 1 time in total.
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: Dates - Last Sunday of the month

Post by t2birkey »

It is also alot cleaner

Code: Select all

 
    function lastSunday(){
        $now = time();
        return(date('l', $now) == 'Sunday' && date('n', $now) == date('n', strtotime('+1 week', $now)));
    }
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Dates - Last Sunday of the month

Post by Weirdan »

My take on this:

Code: Select all

function getLastSunday() {
   return date("Y-m-d", strtotime("last sunday", mktime(0,0,0,date("n")+1,1)));
}
function isTodayLastSunday() {
    return date("Y-m-d") == getLastSunday();
}
 
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Dates - Last Sunday of the month

Post by Darhazer »

All of you are using strtotime()? I can't believe it!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Dates - Last Sunday of the month

Post by Weirdan »

Darhazer wrote:I can't believe it!
Why?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Dates - Last Sunday of the month

Post by Darhazer »

Weirdan wrote:
Darhazer wrote:I can't believe it!
Why?
* strtotime() is slow
* strtotime() is the function, which implementation was changed most
* every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
* Can you predict the behavior of strtotime()? If strtotime("01-02-03") is 01 Feb 2003, 02 Jan 2003, 02 Mar 2001, 03 Feb 2001 or what? (please answer without running it and without looking in php.net)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Dates - Last Sunday of the month

Post by mikemike »

Darhazer wrote:* Can you predict the behavior of strtotime()? If strtotime("01-02-03") is 01 Feb 2003, 02 Jan 2003, 02 Mar 2001, 03 Feb 2001 or what? (please answer without running it and without looking in php.net)
You'd imagine it'd be dependant on locale, but who knows
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Dates - Last Sunday of the month

Post by pickle »

Darhazer wrote:* strtotime() is slow
That depends on what you're trying to do. In some cases it could be quicker to call strtotime('next tuesday',$timestamp) than trying to figure that out with math
Darhazer wrote:* strtotime() is the function, which implementation was changed most
I have no idea what you mean by this.
Darhazer wrote:* every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
PHP is written in English - developers need to learn some English to use it. Plus, if I'm an English speaker writing code for myself, why should I bother being language agnostic?
Darhazer wrote:* Can you predict the behavior of strtotime()? If strtotime("01-02-03") is 01 Feb 2003, 02 Jan 2003, 02 Mar 2001, 03 Feb 2001 or what? (please answer without running it and without looking in php.net)
That's what documentation is for - checking things you don't know. Do you know the order of arguments for strpos() without looking them up? in_array()? ltrim()? You shouldn't expect to have the whole language memorized.

Now for the real reason why I personally use strtotime() as much as possible:

Can you always predict what this would give you?

Code: Select all

$now = time();
$one_hour_from_now = $now + 3600;
What about this:

Code: Select all

$now = time();
$one_hour_from_now = strtotime('+ 1 hour',$now);
Now run both of those 1 second before daylight saving time kicks in/out. You'll get 2 different answers. strtotime() will be the correct answer.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Dates - Last Sunday of the month

Post by Darhazer »

There is a big difference between the need to look at the documentation to see how you should write a line of code, and to see in the documentation (and actually it is not in the documentation, but in the user comments), to understand what a line of code does. If you have to lookup every single line in the documentation to understand a piece of code, the code can be labeled 'unreadable'.
That depends on what you're trying to do. In some cases it could be quicker to call strtotime('next tuesday',$timestamp) than trying to figure that out with math
Quicker for writing, or for the interpreter to run it?

Well, just read the user notes in the php.net for that function, that's a lot of useful info.
Actually the documentation of the function is your experience with it and it changes from version to version.

P.S. The core of the problem is that this function tries to understand English and a computer is not supposed to understand human languages.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Dates - Last Sunday of the month

Post by Weirdan »

Darhazer wrote: * strtotime() is slow
Who cares? It's not a kind of code you would be running in a loop, so shaving off microseconds here won't buy you much performance overall. Get pragmatic, really.
Darhazer wrote: * strtotime() is the function, which implementation was changed most
I didn't know that. But would the output of above snippets be different in different php versions? I doubt it, though the burden of proof still lies on you :)
Darhazer wrote: * every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
1. Date manipulations are hard. Not every programmer is able to get them right. Most are unable, in fact. I've met many experienced programmers who were genuinely confused when they were given simple task of converting arbitrary UTC date/time to user-defined timezone. Many used current user's offset from UTC (as silly as it sounds, but they did).
2. Somehow you seem to think English is easier than 'programming manner'... I'm not a native English speaker, so I don't think it applies to me (can't say for others).
Darhazer wrote: * Can you predict the behavior of strtotime()?
For the code posted here before you asked? Sure I can.
Darhazer wrote: If strtotime("01-02-03")
Years are four-digit currently, every developer should know that already (remember y2k problem?). And, given such string, how would you convert it to timestamp? If input is ambiguous the output is unpredictable unless you happen to know how the algorithm uses it - and that what documentation is for.
Besides, it doesn't resemble the way strtotime was used by me and previous posters.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Dates - Last Sunday of the month

Post by pickle »

Darhazer wrote:this function tries to understand English
Technically it doesn't try to parse the meaning out of any arbitrary English string. I think it only parses proper GNU date strings. There used to be a link to the document outlining what is allowed, but I can't find it any more.

Long story short - it's not as bad as firing up a language parser every time strtotime() is called.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Dates - Last Sunday of the month

Post by Weirdan »

pickle wrote:There used to be a link to the document outlining what is allowed, but I can't find it any more.
It was a link to a chapter of GNU Tar Manual, I believe. Though reading the changelog suggests the function was rewritten in 5.1.0 so I don't know if the information from tar manual still applies (but I guess it does).
Post Reply