Dates - Last Sunday of the month
Moderator: General Moderators
Dates - Last Sunday of the month
Looking for a way to find if today is the last Sunday of the month.
Re: Dates - Last Sunday of the month
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;
Re: Dates - Last Sunday of the month
ty much, I will give this a try!
Re: Dates - Last Sunday of the month
This is the solution I came up with:
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.
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.';
}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.
Re: Dates - Last Sunday of the month
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
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
All of you are using strtotime()? I can't believe it!
Re: Dates - Last Sunday of the month
Why?Darhazer wrote:I can't believe it!
Re: Dates - Last Sunday of the month
* strtotime() is slowWeirdan wrote:Why?Darhazer wrote:I can't believe it!
* 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
You'd imagine it'd be dependant on locale, but who knowsDarhazer 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)
Re: Dates - Last Sunday of the month
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 mathDarhazer wrote:* strtotime() is slow
I have no idea what you mean by this.Darhazer wrote:* strtotime() is the function, which implementation was changed most
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:* every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
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.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)
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;Code: Select all
$now = time();
$one_hour_from_now = strtotime('+ 1 hour',$now);Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Dates - Last Sunday of the month
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'.
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.
Quicker for writing, or for the interpreter to run it?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
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
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 slow
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 youDarhazer wrote: * strtotime() is the function, which implementation was changed most
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).Darhazer wrote: * every developer should be able to calculate date periods in programming manner, not using English expressions as '1 week ago'
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).
For the code posted here before you asked? Sure I can.Darhazer wrote: * Can you predict the behavior of strtotime()?
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.Darhazer wrote: If strtotime("01-02-03")
Besides, it doesn't resemble the way strtotime was used by me and previous posters.
Re: Dates - Last Sunday of the month
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.Darhazer wrote:this function tries to understand English
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.
Re: Dates - Last Sunday of the month
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).pickle wrote:There used to be a link to the document outlining what is allowed, but I can't find it any more.