need help with some simple php :)

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
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

need help with some simple php :)

Post by stylezeca »

I have a question

how would I do something to show a calendar of a certain month according to the server DATE


i wanna do something like this

if ($serverdate = october) {
include("october.html");
} elseif ($serverdate = november) {
include("november.html");
} elseif ($serverdate = december) {
include("december.html");
} else {
echo "error";
}

get what im saying? im not exactly sure how to set it up, but how would i set up a date to grab that information from according to the servers date, not the the clients date... thankyou! and if you could help to show the current month and the next month would be very helpful!

example: on the website it would show

'january' and 'february' saying that the month is january according to the servers date.


thankyou!!!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Code: Select all

<?php include(strtolower(date("F")).".html") ?>
That should include() "january.html" or "febuary.html" etc depending on the servers month.
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

yay

Post by stylezeca »

wow works like a charm, mind explaining how it works? and how would i show the next month too? like if the month is JANUARY, how would i also make it show FEBRUARY?

thanks!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

date("F") grabs the current month. For example, this month it'll be January but next month it'll be February.

Wrapping that with strtolower(), makes the month lowercase.

Then, we add .html to the month to create the file name to include.

It will always include the file for the particular month we are in.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Basically the date() function can be used to get any date/time you want... check out [php_man]date[/php_man] for all of the info but "F" will give you the month ie "January".

If you echo date("jS F Y") that will give you something like "1st January 2004".

The strtolower simply turns the string (in this case the name of the month) into lowercase so that you get "january".

If you want to get the next or previous month(s) you can use strtotime() which allows to modify dates. Here's an example...

Code: Select all

<?php

// Today
echo date("jS F Y")."<br />";

// Next month
echo date("jS F Y", strtotime("+1 month", time())."<br />";

// In 500 days, 42 hours, and 600 minutes time (I think this should work)
echo date("jS F Y", strtotime("+500 days +42 hours +600 minutes", time());
?>
Good date/time type things to check out are [php_man]date[/php_man] [php_man]gmdate[/php_man] [php_man]time[/php_man] and [php_man]strtotime[/php_man]


Hope that all helps :)
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

hnnn

Post by stylezeca »

<?php include(strtolower(date("F")).".html") ?>
<?php include(strtolower(date("F",strtotime("+1 month"))).".html") ?>


would that include january and feb?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You nearly got it right. You're missing the time() bit of the second line.

Code: Select all

<?php

// January
include( strtolower(date("F")).".html" );

// Febuary
include( strtolower(date("F", strtotime("+1 month", time()))).".html" );

?>
;)
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

buzzzzzy

Post by stylezeca »

thanks! whats the time() part do? cause it works without it also...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

time() simply gets the current server time as a unix_timestamp.
Check-out [php_man]time[/php_man] for more info.
stylezeca
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2004 4:27 pm

ok

Post by stylezeca »

ok thanks, helped a lot! but now lol

how would i go about making it grab a certain year, like when its year january 2005, how would i make it grab a different january.html maybe like january2005.html instead of january2004.html
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

You'd change the strtotime function to say "+1 year" or "-1 year" instead.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You need to read (and learn) those links I posted (up the top somewhere).. that way you will be able to do this quite easily :)

Code: Select all

include( strtolower(date("FY", strtotime("+1 year", time()))).".html" );
That will include() the file january2005.html.. just change "+1 year" to whatever you need to.
Post Reply