Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I'm trying to create my own calendar with just php, but I'm running into a problem. This is the top part of my calendar where I have 2 icons. one backwards and one forward arrow. just like in any calendar I want to be able to press those buttons and that the code will change the month to , in this case if you press the backward icon, April. The opposite for the forward arrow.
$sql = "SELECT * FROM reserveringen";
			$stmt = $conn->prepare($sql); 
			$stmt->execute();
			$result = $stmt->get_result();
			while ($row = $result->fetch_assoc()) {
				$start_datum = $row['start_datum'];
				$eind_datum = $row['eind_datum'];
				
				echo $start_datum;
				echo "<br>";
				echo $eind_datum;

			}
			// Get the current date
			// //dit hieronder is dus een array maar volgens mij kan het ook anders
			// $date = getdate();
			//print_r($date);

			//$date = date("d-m-Y");
			$date = new DateTime();

			//function for the buttons
			// if (isset($_POST['currentMonth'])) {
			// 	// kan zijn dat dit er nog in zou moeten $_POST['currentMonth']
			// 	$date = new DateTime();
			// }
			if (isset($_POST['monthBack'])) {
				$date = $date->add(new DateInterval('-1 month'));
			}
			else if (isset($_POST['monthFuture'])) {
				//add 1 month??
				$date = $date->add(new DateInterval('P1W2D'));
			}
			
			//date for above calender
			$calendarTiming = $date->format('d-m-Y');

			// functie voor de maand
			$currentMonth = $date->format('m');

			//check welk jaar het is
			$year = $date->format('Y');

			// hoeveel dagen zitten er in de maand
			$dagenCheck = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $year);

			echo "<br>";

			echo "<thead>";
			echo "<tr>";
			echo "<th colspan='7'>
				<form method='POST' action=''>
				<input type='hidden' name='currentMonth' value='$currentMonth'>
					<button type='submit' name='monthBack'>
						" style="font-size: 20px">
					</button>";
						 print_r($calendarTiming);
						 echo " ". 
					"<button type='submit' name='monthFuture'>
						^__i class='fa fa-angle-right' style='font-size: 20px;'>
					</button>
				</form>
			</th>";
			echo "</tr>";
			echo "<br>";


but this code only works for one month back ward and/or forward.
so the only months I'm getting are 18-04-2022 , 18-05-2022 and 18-06-2022. But I want to be able to just scroll trough the months like and actual online calendar. is this possible in php?
it also doesn't update itself in this part
echo "<th colspan='7'>
				<form method='POST' action=''>
				<input type='hidden' name='currentMonth' value='$currentMonth'>
					<button type='submit' name='monthBack'>
						" style="font-size: 20px">
					</button>";
						 print_r($calendarTiming);
						 echo " ". 
					"<button type='submit' name='monthFuture'>
						^__i class='fa fa-angle-right' style='font-size: 20px;'>
					</button>
				</form>
			</th>";

it just keeps it the same date

What I have tried:

Tried to look it up on stack overflow but I got no results.
I also tried to put the if isset month back and the month future above the thead with the other date functions
but that only gives
18-04-2022 or 18-06-2022
Posted
Updated 30-May-22 10:18am
v3

1 solution

You need to submit the current month with the form, and use that to initialize the date.
PHP
<?php 
    $date = new DateTime();
    if (isset($_POST['currentMonth']) {
        $date = new DateTime($_POST['currentMonth']);
    }
    if (isset($_POST['monthBack']) {
        $date = $date->Add(new DateInterval('-1 month'));
    }
    else if (isset($_POST['monthFuture'])) {
        $date = $date->add(new DateInterval('+1 month'));
    }
    
    $currentMonth = $date->format('d-m-Y');

    ...
    echo "<form method='POST' action=''>
        <input type='hidden' name='currentMonth' value='$currentMonth'>
        <button type='submit' name='monthBack'>...</button>
        $currentMonth
        <button type='submit' name='monthFuture'>...</button>
    </form>";
 
Share this answer
 
v2
Comments
minoesje minoes 18-May-22 5:38am    
it's giving me Fatal error: Uncaught TypeError: date_add(): Argument #1 ($object) must be of type DateTime tried to fix it with new DateTime(); but this gives Fatal error: Uncaught Error: Object of class DateTime could not be converted to string
Richard Deeming 18-May-22 5:49am    
I suspect it's because the $date variable is a string rather than a DateTime.

I've updated my solution.
minoesje minoes 30-May-22 16:16pm    
I know It's been a while but I have been trying this but It's not adding of removing a month it Just keeps IT the same current date
minoesje minoes 30-May-22 16:16pm    
I updated my question to show where I am right now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900