Get next 4 thursdays
Moderator: General Moderators
-
php_beginner
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 16, 2007 1:17 am
Get next 4 thursdays
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.
-
php_beginner
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 16, 2007 1:17 am
Very quick and dirty way of working it out...
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);
?>-
php_beginner
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 16, 2007 1:17 am
Thank you onion2k. What do you think about this?
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>";
}
?>-
php_beginner
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 16, 2007 1:17 am
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Here are a few different ways of using strtotime()
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.
There's too many variables to trust to a 'magic' function like strtotime(), and writing tests for all possible date scenarios would be a massive amount of work. I'd prefer to code something that I can see will work because it's all in front of me. Of course, you should still test it properly, preferably with a series of unit tests.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact: