varad mayee varad mayee - 4 months ago 16
PHP Question

Show calendar from custom custom date

Hii i am working on portal which requires calendar from custom dates like from 14 Jul to 14 Aug.I have made a demo which starts properly i.e.if 14th july starts from Thursday but not ending at sunday which is on 14 aug. Take a look what i have done

function getCalender($year = '',$month = '')
{
$startDate = "2016-07-03";
$endDate = "2016-07-31";
$dateYear = ($year != '')?$year:date("Y");
$dateMonth = ($month != '')?$month:date("m");
$date = $dateYear.'-'.$dateMonth.'-01';
$currentMonthFirstDay = date("N",strtotime($date));
$startDateFrom = date("N",strtotime($startDate));
$endDateFrom = date("N",strtotime($endDate));
$timestamp = strtotime($startDate);
$dayCount = date("d", $timestamp);
$timestamp1 = strtotime($endDate);
$dayCountend = date("d", $timestamp1);

$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
//$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
//$boxDisplay = 29

?>


<div id="calender_section">
<h2>
<!--<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' - 1 Month')); ?>','<?php echo date("m",strtotime($date.' - 1 Month')); ?>');">&lt;&lt;</a>-->
<select name="month_dropdown" class="month_dropdown dropdown"><?php echo getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo getYearList($dateYear); ?></select>
<!--<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' + 1 Month')); ?>','<?php echo date("m",strtotime($date.' + 1 Month')); ?>');">&gt;&gt;</a>-->
</h2>
<div id="event_list" class="none"></div>
<!--For Add Event-->
<div id="event_add" class="none">
<p>Add Event on <span id="eventDateView"></span></p>
<p><b>Event Title: </b><input type="text" id="eventTitle" value=""/></p>
<input type="hidden" id="eventDate" value=""/>
<input type="button" id="addEventBtn" value="Add"/>
</div>
<div id="calender_section_top">
<ul>
<li>Mon</li>
<li>Tue</li>
<li>Wed</li>
<li>Thu</li>
<li>Fri</li>
<li>Sat</li>
<li>Sun</li>
</ul>
</div>
<div id="calender_section_bot">
<ul>
<?php

for($cb=1;$cb<=$dayCountend;$cb++){
//echo $cb;
if(($cb >= $startDateFrom) && $cb<=$dayCountend)
{
/*if(($cb >= $currentMonthFirstDay+1 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){*/
//Current date
$currentDate = $dateYear.'-'.$dateMonth.'-'.$dayCount;
$currentdate = date("D M j",strtotime($currentDate));
$eventNum = 0;
//Include db configuration file
include 'dbConfig.php';
//Get number of events based on the current date
$result = $db->query("SELECT title FROM events WHERE date = '".$currentDate."' AND status = 1");
$eventNum = $result->num_rows;
//Define date cell color
if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
echo '<li date="'.$currentDate.'" class="grey date_cell">';
}elseif($eventNum > 0){
echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
}else{
echo '<li date="'.$currentDate.'" class="date_cell">';
}

//Date cell
echo '<span>';
echo $cb;
echo '</span>';

//Hover event popup
/*echo '<div id="date_popup_'.$currentDate.'" class="date_popup_wrap none">';
echo '<div class="date_window">';
echo '<div class="popup_event">Events ('.$eventNum.')</div>';
echo ($eventNum > 0)?'<a href="javascript:;" onclick="getEvents(\''.$currentDate.'\');">view events</a><br/>':'';
//For Add Event
echo '<a href="javascript:;" onclick="addEvent(\''.$currentDate.'\');">add event</a>';
echo '</div></div>';

echo '</li>';*/
$dayCount++;
?>
<?php }else{ ?>
<li><span>&nbsp;</span></li>
<?php } } ?>
</ul>
</div>
</div>


<?php
}
?>

Answer

I tried for your code and i found one logic i took start date and end date from the user and made the array of the dates which are present between two. Then calendar should start from for ex. 3 Jul 2016 the day is Sunday for that i inserted - upto first date. Check below what i have done

function getCalender($year = '',$month = '',$startDate = '',$endDate = '')
{
$finalArray = array();
    $startDate = "2016-07-03";
    $endDate = "2016-08-31";
    $date1 = createDateRangeArray('2016-07-03 ','2016-08-31');

    //$date = $dateYear.'-'.$dateMonth.'-01';

    $startDateFrom = date("N",strtotime($startDate));
    $endDateFrom = date("N",strtotime($endDate));

    //Take the day from the date
    $timestamp = strtotime($startDate);
    $dayCount = date("d", $timestamp);

    //Take the month from the date
    $timestampend = strtotime($endDate);
    $dayCountend = date("d", $timestampend);

    //Take the month from the date
    $timestampmonth = strtotime($startDate);
    $monthstart = date("m", $timestampmonth);

    //Take the month from the date
    $timestampmonthend = strtotime($endDate);
    $monthend = date("m", $timestampmonthend);

    //Take the year from the start date
    $yearstart = strtotime($startDate);
    $yearofStart = date("y", $yearstart);

    //Take the year from the end date
    $yearend = strtotime($endDate);
    $yearofEnd = date("y", $yearend); 

    //cal difference between start date and month
    $differenceStartDate = $dayCount - 1;

    //cal difference between end date and month
    $totalDaysOfMonthEnd = cal_days_in_month(CAL_GREGORIAN,$monthend,$yearofEnd);
    $totalDaysOfMonthStart = cal_days_in_month(CAL_GREGORIAN,$monthstart,$yearofEnd);

    if($monthstart == $monthend)
    {
        echo "Show one month calendar";
    }
    else
    {
        $diffmonth = $monthend-$monthstart;
    }

    $differenceStartDate = $dayCountend - $totalDaysOfMonth;


    for($i=1;$i<$startDateFrom;$i++)
    {
        $date[] = '-';
    }
    array_push($date,$date1);

    for($j=0;$j<count($date);$j++)
    {
        if(strlen($date[$j])==1)
        {
            array_push($finalArray,$date[$j]);
        }
        else
        {
            for($k=0;$k<count($date[$j]);$k++)
            {
                array_push($finalArray,$date[$j][$k]);
            }
        }
    }
    print_r($finalArray);



    $timestamp1 = strtotime($endDate);
    $dayCountend = date("d", $timestamp1);

    $now = strtotime($startDate);
    $your_date = strtotime($endDate);
    $datediff = $your_date - $now;
    $diff= floor($datediff/(60*60*24)); 

    $totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
    $totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
    //$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
    $boxDisplay = $diff;

?>
    <div id="calender_section">
        <h2>
            <!--<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' - 1 Month')); ?>','<?php echo date("m",strtotime($date.' - 1 Month')); ?>');">&lt;&lt;</a>-->
           <!-- <select name="month_dropdown" class="month_dropdown dropdown"><?php echo getAllMonths($dateMonth); ?></select>
            <select name="year_dropdown" class="year_dropdown dropdown"><?php echo getYearList($dateYear); ?></select>-->
            <!--<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' + 1 Month')); ?>','<?php echo date("m",strtotime($date.' + 1 Month')); ?>');">&gt;&gt;</a>-->
        </h2>
        <div id="event_list" class="none"></div>
        <!--For Add Event-->
        <div id="event_add" class="none">
            <p>Add Event on <span id="eventDateView"></span></p>
            <p><b>Event Title: </b><input type="text" id="eventTitle" value=""/></p>
            <input type="hidden" id="eventDate" value=""/>
            <input type="button" id="addEventBtn" value="Add"/>
        </div>
        <div id="calender_section_top">
            <ul>
                <li>Mon</li>
                <li>Tue</li>
                <li>Wed</li>
                <li>Thu</li>
                <li>Fri</li>
                <li>Sat</li>
                <li>Sun</li>
            </ul>
        </div>
        <div id="calender_section_bot">
            <ul>
            <?php 

                for($cb=0;$cb<count($finalArray);$cb++)
                {
                    //echo $cb;
                    $date = $finalArray[$cb];
                    $month = strtotime($date);
                    $month = date("m", $month);


                    if(($cb >= $startDateFrom -1))
                    {
                    /*if(($cb >= $currentMonthFirstDay+1 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){*/
                        //Current date
                        $currentDate = $finalArray[$cb];
                        //$currentdate = date("D M j",strtotime($currentDate));
                        $eventNum = 0;

                        //Include db configuration file
                        //Define date cell color
                        if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
                            echo '<li date="'.$currentDate.'" class="grey date_cell">';
                            echo '<select name="select"></select>
                                 <option value="0"></option>';
                        }elseif($eventNum > 0){
                            echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
                            echo '<select name="select"></select>
                                 <option value="0"></option>';
                        }else{
                            echo '<li date="'.$currentDate.'" class="date_cell">';
                            echo '<select name="select"></select>
                                 <option value="0"></option>';
                        }

                        //Date cell
                        echo '<span>';
                        echo $currentDate;
                        echo '</span>';
                        $dayCount++;
            ?>
            <?php }else{ ?>
                <li><span>&nbsp;</span></li>
            <?php } } ?>
            </ul>
        </div>
    </div>
<?php
}
function createDateRangeArray($strDateFrom,$strDateTo)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

?>

Take a look at a code and you will understand that easily

Comments