mktime 1st thursday of every month?
Moderator: General Moderators
mktime 1st thursday of every month?
Hi guys, can someone please tell me how I would use mktime to get the first thursday of every month?
Thanks in advance
Thanks in advance
Re: mktime 1st thursday of every month?
Code: Select all
function GetFirstThursday( $month, $year )
{
for ($day=1; $day<=7; $day++)
{
$t = mktime(0,0,0,$month,$day,$year);
if (date('N',$t)==4) return $t;
}
}Re: mktime 1st thursday of every month?
Thanks for that,
However, what I am trying to achieve is, on my site I need to always show "On Thursday {{{SOMEPHP}}}, there will be an event..."
I am not sure how to get that function working, it doesnt seem to display anything when called.
Thanks again.
However, what I am trying to achieve is, on my site I need to always show "On Thursday {{{SOMEPHP}}}, there will be an event..."
I am not sure how to get that function working, it doesnt seem to display anything when called.
Thanks again.
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: mktime 1st thursday of every month?
Function Apollo wrote isn't supposed to display anything, it returns the timestamp for the 1st thursday in month. It's your job to display the returned value. Something like this
I strongly suggest you read up on functions and PHP language reference in general, since this is pretty basic stuff that had you confused.
Code: Select all
$thursday = GetFirstThursday(9, 2008);
echo "On Thursday " . date('m/d/Y', $thursday) . ", there will be an event...";Re: mktime 1st thursday of every month?
Thanks for that also, I did try using
But was not recieving any return, when trying your sample all I get is On Thursday 01/01/1970, there will be an event... Everytime!
Any ideas?
PS I am quite sure I know how to use functions
, but thanks fo the tip 
Code: Select all
$m = date('m');
$y = date('Y');
$thursday = GetFirstThursday($m, $y);
But was not recieving any return, when trying your sample all I get is On Thursday 01/01/1970, there will be an event... Everytime!
Any ideas?
PS I am quite sure I know how to use functions
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: mktime 1st thursday of every month?
Well, you're doing something wrong, cause both with your code snippet and with mine, I got appropriate results.
returns
Code: Select all
$m = date('m');
$y = date('Y');
$thursday = GetFirstThursday($m, $y);
var_dump(date('m/d/Y', $thursday));
$thursday = GetFirstThursday(9, 2008);
echo "On Thursday " . date('m/d/Y', $thursday) . ", there will be an event...";Code: Select all
string '08/07/2008' (length=10)
On Thursday 09/04/2008, there will be an event...Re: mktime 1st thursday of every month?
Yup, there is something strange going on here 
This
returns
On localhost
and on remote server returns this:
Not sure what's going on here 
Thanks for your help.
This
Code: Select all
<?php
function GetFirstThursday( $month, $year )
{
for ($day=1; $day<=7; $day++)
{
$t = mktime(0,0,0,$month,$day,$year);
if (date('N',$t)==4) return $t;
}
}
$m = date('m');
$y = date('Y');
$thursday = GetFirstThursday($m, $y);
var_dump(date('m/d/Y', $thursday));
$thursday = GetFirstThursday(9, 2008);
echo "On Thursday " . date('m/d/Y', $thursday) . ", there will be an event...";
?>
Code: Select all
string(10) "08/07/2008" On Thursday 09/04/2008, there will be an event...
and on remote server returns this:
Code: Select all
string(10) "01/01/1970" On Thursday 01/01/1970, there will be an event...
Thanks for your help.
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: mktime 1st thursday of every month?
The problem might be date('N'), if you're using the PHP version < PHP 5.1.0, since thats when 'N' format parameter was added. Try using
in the function.
Code: Select all
if (date('w',$t) == 3) return $t;Re: mktime 1st thursday of every month?
Yes, seems the remote server is php4 (OO), thanks for your help mate, much appreciated 
Re: mktime 1st thursday of every month?
You were using lowercase 'y' to get the year, which returns two digits, e.g. '08' (which is numerically equal to 8 ) instead of '2008'.ianhull wrote:But was not recieving any return, when trying your sample all I get is On Thursday 01/01/1970, there will be an event... Everytime!
Any ideas?
Use $y=date('Y') (note the capital 'Y')
Also, use date('j') instead of date('m') to get the month without a leading zero. Although it's a string here, the leading zero may cause it to be mistaken as an octal value (especially if you copy paste values to your code later on or whatever).
Re: mktime 1st thursday of every month?
You could also use strtotime() I'm pretty sure.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.