Page 1 of 2

Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 5:07 pm
by t2birkey
Looking for a way to find if today is the last Sunday of the month.

Re: Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 5:26 pm
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

Re: Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 5:29 pm
by t2birkey
ty much, I will give this a try!

Re: Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 5:34 pm
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.

Re: Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 5:42 pm
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)));
    }
 

Re: Dates - Last Sunday of the month

Posted: Thu Jun 04, 2009 6:17 pm
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();
}
 

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 2:16 pm
by Darhazer
All of you are using strtotime()? I can't believe it!

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 2:29 pm
by Weirdan
Darhazer wrote:I can't believe it!
Why?

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 2:58 pm
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)

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 3:19 pm
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

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 3:26 pm
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.

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 3:48 pm
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.

Re: Dates - Last Sunday of the month

Posted: Fri Jun 05, 2009 8:21 pm
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.

Re: Dates - Last Sunday of the month

Posted: Mon Jun 08, 2009 4:39 pm
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.

Re: Dates - Last Sunday of the month

Posted: Mon Jun 08, 2009 4:48 pm
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).