Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Paul Sawchuk
397 PointsPHP date function
Hi All, I'm a php newbe and want to change a quote form to display a new quote every week rather that every month. I have the code below and have tried a whole bunch of variations based on trial and error. Any help would be appreciated.
Here's the code:
/////////////////////////////////////////////////////////
<?php
$today = getdate();
$month = $today['month'];
if($month == 'January')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'February')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'March')
{
echo "Man often becomes what he believes himself to be. If I keep on saying to myself that I cannot do a certain thing, it is possible that I may end by really becoming incapable of doing it. On the contrary, if I have the belief that I can do it, I shall surely acquire the capacity to do it even if I may not have it in the beginning. <br><em>- </em>Mahatma Gandhi 1869-1948";
}
if($month == 'April')
{
echo "Man often becomes what he believes himself to be. If I keep on saying to myself that I cannot do a certain thing, it is possible that I may end by really becoming incapable of doing it. On the contrary, if I have the belief that I can do it, I shall surely acquire the capacity to do it even if I may not have it in the beginning. <br><em>- </em>Mahatma Gandhi 1869-1948";
}
if($month == 'May')
{
echo "Man often becomes what he believes himself to be. If I keep on saying to myself that I cannot do a certain thing, it is possible that I may end by really becoming incapable of doing it. On the contrary, if I have the belief that I can do it, I shall surely acquire the capacity to do it even if I may not have it in the beginning. <br><em>- </em>Mahatma Gandhi 1869-1948";
}
if($month == 'June')
{
echo "Man often becomes what he believes himself to be. If I keep on saying to myself that I cannot do a certain thing, it is possible that I may end by really becoming incapable of doing it. On the contrary, if I have the belief that I can do it, I shall surely acquire the capacity to do it even if I may not have it in the beginning. <br><em>- </em>Mahatma Gandhi 1869-1948";
}
if($month == 'July')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'August')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'September')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'October')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'November')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
if($month == 'December')
{
echo "Luck is where preparation meets opportunity.
? Randy Pausch";
}
?>
2 Answers
Geoff Parsons
11,679 PointsYou might want to check out the date function for some options. Passing "W" to date will return the week of the year (1-52) and you could use this to return a different quote.
$weekOfYear = date("W");
If you go with this approach i'd recommend just putting the quotes in an array and indexing them based on the value returned by date().
Paul Sawchuk
397 PointsHi Geoff, Thanks for the information. Coule you please give me an extended example? As I say I'm a PHP neophyte and don't really get wha you're saying re the date setup.
Thanks,
Paul