changing day of the month

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
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

changing day of the month

Post by garry27 »

i have a calendar with two hyperlinks in it. one takes you to the next month, the other to the previous month. the code below performs this function but when it comes to changing the name of the month ('F') it ony returns a useless integer value.


Code: Select all

$month = $_GET['month'];
		$month_name = $_GET['month_name'];
		if ($month == '')  
		{
		$month = date("m");
		$month_name = date("F");
		}
    
    if ($month > 1) 
      $prevm = sprintf ( ' <a href="?month=%s&month_name=%s&year=%s" ><<</a> ', $month - 1, $month_name - 1, $year );
    else      //If it is January our prev link will be December of the previous year 
    $prevm = sprintf ( ' <a href = "?month=%s&month_name=%s&year=%s" ><<</a> ' , 12, $month_name - 1, $year - 1 ); 
    if ($month < 12) 
      $nextm = sprintf ( ' <a href="?month=%s&month_name=%s&year=%s" >>></a>', $month + 1, $month_name + 1, $year );
    else   //if it is December our next link will be January of the next year
      $nextm = sprintf( ' <a href="?month=%s&month_name=%s&year=%s" >>></a>', 1, $month_name + 1, $year + 1 );

how do i get it so it changes the string name of the current month and how do i make the top line to read "if no post value for 'month' was passed"? so it looks neater. i tried

Code: Select all

if (!$_POST['month'])
but it didn't work

thanks in advance!

garry
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm not sure what you are asking, please rephrase.

As for

Code: Select all

if (!$_POST['month'])
Try using empty()

Code: Select all

if (empty($_POST['month'])) {
although I think you meant to use $_GET
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Month name

Post by churt »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


"$month_name" is a text string and can't be used in calculation.  You are already passing the month number you want ("$month ").   Just reset the month name on the other end useing the month variable you passed.

Code: Select all

$month = $_GET['month'];
                $month_name = $_GET['month_name'];
                if ($month == '') 
                {
                $month = date("m");
                $month_name = date("F");
                }
    if ($month > 1)
      $prevm = sprintf ( ' <a href="?month=%s&month_name=%s&year=%s" ><<</a> ', $month - 1, $year );
    else      //If it is January our prev link will be December of the previous year
    $prevm = sprintf ( ' <a href = "?month=%s&month_name=%s&year=%s" ><<</a> ' , 12, $year - 1 );
    if ($month < 12)
      $nextm = sprintf ( ' <a href="?month=%s&month_name=%s&year=%s" >>></a>', $month + 1, $year );
    else   //if it is December our next link will be January of the next year
      $nextm = sprintf( ' <a href="?month=%s&month_name=%s&year=%s" >>></a>', 1, $year + 1 );
Somthing like:

Code: Select all

$month_name=date("F", mktime(0,0,0,$month);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply