Get next 4 thursdays
Posted: Thu May 10, 2007 2:49 am
How can i get dates for next 4 Thursdays including today if its a Thursday?
I am using PHP 4.4 and MySQL 4.1.
I am using PHP 4.4 and MySQL 4.1.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
$day = 10;
$month = 5;
$year = 2007;
$dayOfTheWeek = date("w", mktime(0,0,0,$month,$day,$year)); //0 is Sunday, so 4 is Thursday
switch ($dayOfTheWeek)
{ //Work out how many days till next Thursday. This is crap, there's probably a neater method.
case 4: $daysToAdd = 0; break;
case 5: $daysToAdd = 6; break;
case 6: $daysToAdd = 5; break;
case 0: //Fall through
case 1: //Fall through
case 2: //Fall through
case 3: $daysToAdd = 4-$dayOfTheWeek; break;
}
for ($x=0;$x<4;$x++)
{
$thursday[$x] = date("d/m/Y", mktime(0,0,0,$month,$day+($daysToAdd+($x*7)),$year));
}
var_dump($thursday);
?>Code: Select all
<?php
$a_Thursdays = array();
for ($i_Count = 0 ; $i_Count < 4 ; ++$i_Count)
{
$a_Thursdays[] = strtotime("thursday +{$i_Count} weeks");
}
foreach($a_Thursdays as $dt_Thursday)
{
echo date('r', $dt_Thursday), "<br>";
}
?>Can you please explain a bit?onion2k wrote:I never use strtotime() because it's a bit mysterious ... there's no way to be certain what it'll do.
Code: Select all
echo date('l m d',strtotime('this thursday')).'<br/>';
echo date('l m d',strtotime('next thursday')).'<br/>';
echo date('l m d',strtotime('+2 thursday')).'<br/>';
echo date('l m d',strtotime('thursday + 3 weeks')).'<br/>';If I tell strtotime() to give me "thursday +1 week" and it's Thursday today, do I mean including today? Not including today? Does it work if today is a leap day (Feb 29th)? What if it's a day that rolls over the Daylight Savings date?php_beginner wrote:Can you please explain a bit?onion2k wrote:I never use strtotime() because it's a bit mysterious ... there's no way to be certain what it'll do.