Page 1 of 1
mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 5:02 am
by ianhull
Hi guys, can someone please tell me how I would use mktime to get the first thursday of every month?
Thanks in advance
Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 5:17 am
by Apollo
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?
Posted: Tue Aug 12, 2008 5:36 am
by ianhull
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.
Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 5:49 am
by EverLearning
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
Code: Select all
$thursday = GetFirstThursday(9, 2008);
echo "On Thursday " . date('m/d/Y', $thursday) . ", there will be an event...";
I strongly suggest you read up on
functions and
PHP language reference in general, since this is pretty basic stuff that had you confused.
Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 5:57 am
by ianhull
Thanks for that also, I did try using
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

, but thanks fo the tip

Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 6:11 am
by EverLearning
Well, you're doing something wrong, cause both with your code snippet and with mine, I got appropriate results.
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...";
returns
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?
Posted: Tue Aug 12, 2008 6:17 am
by ianhull
Yup, there is something strange going on here
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...";
?>
returns
Code: Select all
string(10) "08/07/2008" On Thursday 09/04/2008, there will be an event...
On localhost
and on remote server returns this:
Code: Select all
string(10) "01/01/1970" On Thursday 01/01/1970, there will be an event...
Not sure what's going on here
Thanks for your help.
Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 6:22 am
by EverLearning
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.
Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 6:33 am
by ianhull
Yes, seems the remote server is php4 (OO), thanks for your help mate, much appreciated

Re: mktime 1st thursday of every month?
Posted: Tue Aug 12, 2008 7:13 am
by Apollo
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?
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'.
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?
Posted: Tue Aug 12, 2008 9:57 am
by pickle
You could also use
strtotime() I'm pretty sure.