mktime 1st thursday of every 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

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

mktime 1st thursday of every month?

Post 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
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: mktime 1st thursday of every month?

Post 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;       
 }
}
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Re: mktime 1st thursday of every month?

Post 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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: mktime 1st thursday of every month?

Post 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.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Re: mktime 1st thursday of every month?

Post 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 :)
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: mktime 1st thursday of every month?

Post 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...
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Re: mktime 1st thursday of every month?

Post 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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: mktime 1st thursday of every month?

Post 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

Code: Select all

if (date('w',$t) == 3) return $t;
in the function.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Re: mktime 1st thursday of every month?

Post by ianhull »

Yes, seems the remote server is php4 (OO), thanks for your help mate, much appreciated :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: mktime 1st thursday of every month?

Post 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).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mktime 1st thursday of every month?

Post by pickle »

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.
Post Reply