I have button next and previous which is to get the next and previous totalday month year. I do not have problem to get the next and previous month year but have problem to get the next and previous totalday.
scenario 1: user click button next and page will display total days for next month (no problem here)
scenario 2: user click button previous and page will display total days for previous month (no problem here)
scenario 3: user click button next and page will display total days for next month (no problem here.. )(but..) after user click button next then click button previous, it will display the wrong total days for previous month.
example for scenario 3: total days for current month 'NOVEMBER' is 30 and total days for next month DECEMBER is 31. If user click button next which is will go to the next month then page will display total days 31. but when user click button previous which is go back to the current month, the page display wrong total days which is 31. It should be 30.
code:
<?php
session_start();
include 'theme-head.php';
include 'theme-header.php';
$currentmonth = date('m');
$currentyear = date("Y");
$currentyeartotday = date('t');
$months = array('January','February','March','April','May','June','July','August','September','October','November','December');
//$years = array('2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020');
//sets initial value of SESSION variables to current month, year and total day
if(!isset($_SESSION['month'])){
$_SESSION['month'] = $currentmonth;
}
if(!isset($_SESSION['year'])){
$_SESSION['year'] = $currentyear;
}
if(!isset($_SESSION['totalday'])){
$_SESSION['totalday'] = $currentyeartotday;
}
//increments or decrements year month and totalday based on user input
if(isset($_POST['NextMonth'])){
if ($_SESSION['month']+1 == 13){
++$_SESSION['year'];
}
$_SESSION['month'] = ($_SESSION['month']+1 < 13) ? ++$_SESSION['month'] : 1;
$_SESSION['ic'] = isset($_SESSION['ic']) ? ++$_SESSION['ic'] : 1;
$ic1 = $_SESSION['ic'];
$_SESSION['totalday'] = date('t', strtotime("+$ic1 month ", strtotime($_SESSION['year'])));
}else{
unset($_SESSION['ic']);
}
if(isset($_POST['LastMonth'])){
if($_SESSION['month']-1 == 0){
--$_SESSION['year'];
}
$_SESSION['month'] = ($_SESSION['month']-1 > 0) ? --$_SESSION['month'] : 12;
$_SESSION['dc'] = isset($_SESSION['dc']) ? ++$_SESSION['dc'] : 1;
$dc1 = $_SESSION['dc'];
$_SESSION['totalday'] = date('t', strtotime("-$dc1 month ", strtotime($_SESSION['year'])));
}
else{
unset($_SESSION['dc']);
}
//current year month and totalday based on user input
if(isset($_POST['Today'])){
$_SESSION['year'] = $currentyear;
$_SESSION['month'] = $currentmonth;
$_SESSION['totalday'] = $currentyeartotday;
}
//print_r($_SESSION); //uncomment to keep tabs on your variables
?>
<div class="col-md-2">
<input type="text" placeholder="date month year" readonly>
</div>
<div class="col-md-9">
<div class="well">
<form method="post">
<button class="fa fa-chevron-circle-left btn-primary btn btn-xs" name="LastMonth" type="submit"></button>
<button class="fa fa-chevron-circle-right btn-primary btn btn-xs" name="NextMonth" type="submit"></button>
<?php
if(isset($_POST['NextMonth'])){
?>
<button class="btn-primary btn btn-xs" name="Today" type="submit">this month</button>
<?php }
elseif (isset($_POST['LastMonth'])) {
?>
<button class="btn-primary btn btn-xs" name="Today" type="submit">this month</button>
<?php }
else{
?>
<button class="btn-primary btn btn-xs" name="Today" type="submit" disabled>this month</button>
<?php }?>
<div class="pull-right">
<h2><?php echo ''. $months[$_SESSION['month']-1] .' '. $_SESSION['year'].''?></h2> <?php echo''. $_SESSION['totalday'] .' ' ?>
</div>
</form>
</div>
</div>
I do not have any logic on how to get the exact total days when user click button next and then click button previous.
I know maybe should use IF statement to solve this problem but the logic on how to use the IF statement still dont have that idea.
Aucun commentaire:
Enregistrer un commentaire