Get next 4 thursdays

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
php_beginner
Forum Newbie
Posts: 6
Joined: Mon Apr 16, 2007 1:17 am

Get next 4 thursdays

Post by php_beginner »

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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

can you do what you ask for in pseudo code.
Give us what you have tried.
php_beginner
Forum Newbie
Posts: 6
Joined: Mon Apr 16, 2007 1:17 am

Post by php_beginner »

I have nothing in mind that from where i should start.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

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

Post by php_beginner »

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>";
}
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I never use strtotime() because it's a bit mysterious ... there's no way to be certain what it'll do. I prefer to write code that's very obvious, even if it means being more verbose. But your code works, so go with whatever you like better.
php_beginner
Forum Newbie
Posts: 6
Joined: Mon Apr 16, 2007 1:17 am

Post by php_beginner »

This is not my code. I have found it on some other site.
onion2k wrote:I never use strtotime() because it's a bit mysterious ... there's no way to be certain what it'll do.
Can you please explain a bit?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

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/>';
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

php_beginner wrote:
onion2k wrote:I never use strtotime() because it's a bit mysterious ... there's no way to be certain what it'll do.
Can you please explain a bit?
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?

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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

"this Thursday" (or just "Thursday") does mean today, if today is Thursday. Now that that's out of the way... let the games begin!
Post Reply