Solved

click back to previous months in PHP

Posted on 2014-11-12
7
79 Views
Last Modified: 2014-11-23
I'm trying to get this data to display from month to month in a database. Right now the data is called $month and there is also a date stored in the database by month and also complete date. This is the code that somebody was helping me with. I am very new to PHP. Right now it goes forwards and backwards but doesn't display the current month.

if (!isset($_REQUEST['next']) || !isset($_REQUEST['prev'])) {
	 	$month= ltrim(date('m'),0) ;
	 }
	 $direction = '';
	 if (isset($_REQUEST['next'])){ 
	    $direction = 'next';
	 }
	if (isset($_REQUEST['prev'])){ 
		$direction = 'prev';
	}
	if ($direction == 'next'){
	 	$month = $month + 1;
		print_r($month);
	 } else {
	 	$month = $month - 1;
		print_r($month);
	 }
 	 date_default_timezone_set('America/New_York');




<form method="post" action="/reports.php">
<input type="submit"/>
<input type="hidden" name="prev" val="next"/>
</form>


<form method="post" action="/reports.php">

<input type="submit"/>
<input type="hidden" name="next" val="next"/>
</form>

Open in new window

0
Comment
Question by:designaire
  • 2
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
This article describes how to handle date/time values in PHP and MySQL.  Please read it over and post back if you still have questions about the concepts.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 14

Expert Comment

by:CtrlAltDl
Comment Utility
I don't see how you are keeping track of the current month with the code you supplied.  Unless it is handled in some code that you're not showing here your $month will always be the current month.

In line 1 of your code you are checking to see if Next OR Previous are Not selected.  This will always be True, so I think you want to check if Next AND (&&) Previous are Not selected.
if (!isset($_REQUEST['next']) && !isset($_REQUEST['prev'])) {

Open in new window

Also your "prev" hidden field has a value of "next" which I'm guessing should be a value of "prev".
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
See if this helps.  I may be misunderstanding the question, but as I interpret it the question would be "How do I get the values for the previous or next month?"  If that's not what you want, please give us a little more to go on.
http://iconoun.com/demo/temp_designaire.php

<?php // demo/temp_designaire.php
error_reporting(E_ALL);

// SET THE LOCAL TIMEZONE
date_default_timezone_set('America/New_York');

// ACQUIRE AND NORMALIZE THE REQUEST VARIABLE FROM THE URL
$get = !empty($_GET['q']) ? trim(strtolower(substr($_GET['q'],0,1))) : FALSE;
if ($get)
{
    // TO FIND NEXT MONTH
    if ($get == 'n')
    {
        $next = mktime( 0,0,0, date('m') + 1, date('d'), date('Y') );
        echo PHP_EOL . "NEXT MONTH: " . date('F, Y', $next);
    }
    // TO FIND PREVIOUS MONTH
    elseif ($get == 'p')
    {
        $prev = mktime( 0,0,0, date('m') - 1, date('d'), date('Y') );
        echo PHP_EOL . "PREV MONTH: " . date('F, Y', $prev);
    }
    // IF THERE IS AN UNKNOWN REQUEST
    else
    {
        echo PHP_EOL . "UNRECOGNIZED INPUT: " . $_GET['q'];
    }
}

// CREATE THE REQUEST FORM USING HEREDOC NOTATION
$form = <<<EOD
<form>
What month?
<br>
<input name="q" type="radio" value="Next" /> Next
<br>
<input name="q" type="radio" value="Prev" /> Previous
<br>
<input type="submit" value="Go!" />
</form>
EOD;

echo $form;

Open in new window

0
 

Accepted Solution

by:
designaire earned 0 total points
Comment Utility
Thanks, I got somebody to help me with it...It doesn't go through the years but it will work for what I need it for...

I'll try to look at your code and use it on a later date


$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));

if ($month=="")
{  
	date_default_timezone_set('America/New_York');
	$dateTime = date(DATE_RSS);      
	echo $dateTime . "<br>" . "<br>";
	echo "date('m') =" . date('m') . "<br>" . "<br>";  	 
	$month = ltrim(date('m'),0) ;  	
	echo "ltrim(date('m'),0) =" . $month . "<br>" . "<br>";  // I think that all 
}

$prevmonthtopost = ((($month+11)-1) % 12) + 1;  
$nextmonthtopost = ((($month+11)+1) % 12) + 1;  
?>

<form method="post" action="/next3.php?month=<?php echo $prevmonthtopost ?>">
  <input type="submit" value="previous" name="prev"/>
</form>
<p></p>
<form method="post" action="/next3.php?month=<?php echo $nextmonthtopost ?>">
  <input type="submit" value="nextious month" name="next"/>
</form>

<h1>Month <?php echo $month ?></h1>

<!-- Or you could do it this way -->
    <h1>Days in <?php echo $month ?> you meditated.</h1></div>

Open in new window

0
 

Author Closing Comment

by:designaire
Comment Utility
I got somebody to help me with the problems
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now