Jump to content

How do you find dates of specific days of the week in the future?


Gordon Werner

Recommended Posts

Hi there ...

 

how does one determine the following.

 

I need to have a message pop up every 2nd tuesday ... and I need to be able to store this in advance.

 

if I being with SEP09 ... how do I easily determine what # day is the 2nd tues in OCT or NOV, etc ...

 

I cannot just add seconds from epoch because the # of the day will change from month to month

 

Can anyone help?

 

Thanks in advance

 

Gordon

Try this:

 

$month = 8;
$year = 2008;
$start_of_month = mktime(0,0,0,$month,1,$year);
if(date('N',$start_of_month)==2){//first day of month is tuesday
$second_tuesday = date('d-m-Y',$start_of_month+60*60*24*7);
}else{
$second_tuesday = date('d-m-Y',strtotime('next tuesday',$start_of_month)+60*60*24*7);
}
echo $second_tuesday;


<?php

$tue = date('N',$timestamp);
$day = date('d',$timestamp);

if ( $tue == 2 && $day > 7 ) {
// then it's sure that is the second week and day is tuesday

..more action..

}

?>

 

from the head without testing...

tell me if i'm wrong... i think it's better solution

you can always enter a regular date into the strtotime function

 

$whatDayWillitBNextYear = date("l", strtotime("Sep 17 2009"));

echo $whatDayWillitBNextYear;

 

$whatDayWillitBNextYear = date("l", strtotime("09/17/2030"));

echo $whatDayWillitBNextYear;

 

$whatDayWillitBNextYear = date("l", strtotime("9 17 2010"));

echo $whatDayWillitBNextYear;

 

$whatDayWillitBNextYear = date("l", strtotime("September 17th 2042"));

echo $whatDayWillitBNextYear;

Edit:

<?php

$tue = date('N',$timestamp);
$day = date('d',$timestamp);

if ( $tue == 2 && $day >= 7 ) {
// then it's sure that is the second week and day is tuesday

..more action..

}

?>

 

btw sry for new post, i saw edit icon after posting

Here's another way:

<?php
for ($y=2008;$y<2011;$y++)
for ($m=1;$m<13;$m++) {
	$sec_tues = (date('l',strtotime($y . '-' . $m . '-1')) == 'Tuesday')?date ('F jS, Y',strtotime('first tuesday',strtotime($y . '-' . $m . '-1'))):date ('F jS, Y',strtotime('second tuesday',strtotime($y . '-' . $m . '-1')));
	echo $sec_tues.'<br>';
	}
?>

 

Ken

No. I tried:

<?php
cho date('F jS, Y',strtotime("second tuesday in next month"));
?>

and got

December 31st, 1969

which is what you get when strtotime() doesn't recognize the date string.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.