Jump to content

date conversion and calendar


mrkhoros

Recommended Posts

Hello fellas

 

I have been working on this piece of php for over a week now and I have got a few questions.

 

1. Where is the $selected_date is being read from ? given that this is a unixtime stamp format , should it not get converted with date function ? The code however is working fine with Gregorian calendar but with persian calendar time conversion , the $selected_date returns as year 1970 instead of 2014.

 

 

2. by clicking <a href='calendar.php?month=" . $this->month . "&year=" .  $this->year . "&day=" . sprintf("%02s", $daynumber) . "'>

 

what function is being called ?

 

 

 

** the make_time and mds_date are actually from a separated php class which is responsible for the date year and day conversion

 

 

 

 

 

 

 

 

 

<?php
include 'PersianCalendar.php';
//require_once("PersianCalendar.php");
date_default_timezone_set("Asia/Tehran");
 
 
 
class booking_diary {
 
 
 
// Mysqli connection
function __construct($link) {
 
    $this->link = $link;
}
 
/*
Settings you can change:
 
$booking_start_time:                The time of the first slot
$booking_end_time:                  The time of the last slot 
$booking_frequency:                 The amount of slots per hour, expressed in minutes 
$booking_slots_per_day:             The total number of slots avaliable in one day
*/
    public $current_day;
public $booking_start_time          = "09:00";  
public $booking_end_time            = "19:00";   
public $booking_frequency           = 120;     
public $booking_slots_per_day       = 10;
 
public $day, $month, $year, $selected_date, $first_day, $back, $back_month, $back_year, $forward, $forward_month, $forward_year, $bookings, $count, $days;
 
 
/*========================================================================================================================================================*/
 
 
function make_calendar($selected_date, $first_day, $back, $forward, $day, $month, $year) {
//persian starts here
 
$year = mds_date("Y");
$chand = mds_date("m");
$fdchand = mds_date("D", make_time(0,0,0,$chand,01,$year));
 
 
 
 
 
 
 
//going back persian starts
$curmonth = mds_date("m", time());
$month = mds_date("m");
$curyear = mds_date("Y", time());
 
if($curmonth=='01')
{
$monthnominat = 12;
$yearnominat = $curyear-1;
}
else
{
$monthnominat = $curmonth-1;
$yearnominat = $curyear;
}
echo date("d m Y" ,$selected_date);
 
$this->current_day = $day;
$faselected = mds_date("d, m, Y", $selected_date);
var_dump($faselected);
 
 
$back = make_time(1, 1, 1, $monthnominat, 1, $yearnominat);
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (empty($selected_date)) 
{
 
            $parts = explode(",", $faselected);
            $this->month = $parts[1];
            $this->day = $parts[0];
            $this->year = $parts[2];
$first_day = $this->chandom(mds_date("D", make_time(0, 0, 0, $this->month, 1, $this->year)));
 
$check = 2;
 
}
else
{
 
$this->day = 0;
$this->month = mds_date("m");
            $this->year = mds_date("Y");            
    $first_day = $this->chandom($fdchand);
 
 
$check = 1;
 
}
    // Add a value to these public variables  
echo "<br />";
echo $check;
    $this->back = $back;
    $this->back_month = mds_date("m", $back);
    $this->back_year = mds_date("Y", $back); // Minus one month back arrow
    $this->selected_date = make_time(24, 0, 0, $this->month, $this->day, $this->year);
    $this->first_day = $first_day;
    $this->forward = $forward;
    $this->forward_month = date("m", $forward);
    $this->forward_year = date("Y", $forward); // Add one month forward arrow    
    
    // Make the booking array
    $this->make_booking_array($year, $month);
 
echo mds_date("F Y", $this->selected_date);
}
 
 
function make_booking_array($year, $month) { 
 
 
    $query = "SELECT * FROM bookings WHERE date LIKE '$year-$month%'"; 
    $result = mysqli_query($this->link, $query) or die(mysqli_error($this->link)); 
 
    $this->count = mysqli_num_rows($result); 
    $this->bookings = '';  
 
    while ($row = mysqli_fetch_array($result)) {
 
        $this->bookings[] = array(
            "name" => $row['name'], 
            "date" => $row['date'], 
            "start" => $row['start'],
            "comments" => $row['comments']
            ); 
        }
 
     $this->make_days_array( $this->year, $this->month);   
            
} // Close function
 
 
function make_days_array($year, $month) { 
 
    // Create an array of days in the month                 
    //$num_days_month = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
      $num_days_month =  dim($month, $year);
    // Make array called $day with the correct number of days
    for ($i = 1; $i <= $num_days_month; $i++) {
        $d = make_time(0, 0, 0, $month, $i, $year);
        
        $this->days[] = array("daynumber" => $i, "dayname" => mds_date("l", $d)); 
      
           }
 
    // Add blank elements to start of array if the first day of the month is not a Monday.
    for ($j = 1; $j <= $this->first_day; $j++) {
        array_unshift($this->days, '0'); 
    }
 
    // Add blank elements to end of array if required.
    $pad_end = 7 - (count($this->days) % 7);
 
    if ($pad_end < 7) {
        for ($j = 1; $j <= $pad_end; $j++) {
        array_push($this-> days, '|'); }
    } // Close if
 
    $this->make_table_top();   
 
} // Close function
   
    
function make_table_top() {
 
        echo "
        <div class='left'>
   
        <table border='0' cellpadding='0' cellspacing='0' id='calendar'>
            <tr id='week'>
            <td align='left'><a href='?month=" . mds_date("m", $this->back) . "&year=" . mds_date("Y", $this->back) . "'>«</a></td>
            <td colspan='5' id='center_date'>" . mds_date("F Y", $this->selected_date) . "</td>    
            <td align='right'><a href='?month=" . mds_date("m", $this->forward) . "&year=" . mds_date("Y", $this->forward) . "'>»</a></td>
            
           
        </tr>
        <tr>
            <th>شنبه</th><th>یکشنبه</th><th>دوشنبه</th><th>سه شنبه</th><th>چهارشنبه</th><th>پنجشنبه</th><th>جمعه</th>";
            
        $this->make_day_boxes($this->days, $this->bookings, $this->month, $this->year);
    
    } // Close function
 
 
function make_day_boxes() {
 
        $i=0;
 
        foreach ($this->days as $row) {
 
            $tag = ''; 
 
            if($i % 7 == 0)
                echo "</tr><tr>"; // Use modulus to give us a <tr> after every seven <td> cells
 
            if(isset($row['daynumber']) && $row['daynumber'] != 0) { // Padded days at the start of the month will have a 0 at the beginning
 
                echo "<td width='21' valign='top' class='days'>";
 
                if ($this->count > 0) {
                    
                    $day_count = 0;                    
                    foreach ($this->bookings as $booking_date) {
 
                        if ($booking_date['date'] == $this->year . '-' . $this->month . '-' . sprintf("%02s", $row['daynumber'])) {
                        $day_count++;           
                        } // Close if
              
                    } // Close foreach
                    
                } // Close if $count
 
                // Work out which colour day box to show
                                          
                if ($row['dayname'] == 'جمعه')
                    $tag = 2; // It's a friday
 
                if (make_time(0, 0, 0, $this->month, sprintf("%02s", $row['daynumber']) + 1, $this->year) < strtotime("now"))
                    $tag = 4; // Past Day  
                                        
                if ($day_count >= $this->booking_slots_per_day && $tag == '') 
                    $tag = 3;                    
              
                if ($day_count >0 && $tag == '')         
                    $tag = 1;           
                                             
                echo $this->day_switch($tag, $row['daynumber']) . 
 
                "<span>" . str_replace('|', ' ', $row['daynumber']) . "</span></td>";
 
            } else {
                echo "<td width='21' valign='top' class='days'><img src='images/block_null.gif' title='مجاز به انتخاب نیستید' alt=''></td>";
            }
            
            $i++;
            
        } // Close foreach
        
    $this->make_key(); 
        
} // Close function
 
 
function day_switch($tag, $daynumber) {
 
        switch ($tag) {
        
        case (1): // Part booked day
            $txt = "<a href='calendar.php?month=" . $this->month . "&year=" .  $this->year . "&day=" . sprintf("%02s", $daynumber) . "'>
                    <img src='images/block_part.gif' title='ساعاتی رزرو شده' border='0' alt=''></a>\r\n";
            break;
 
        case (2): // friday
            $txt = "<img src='images/block_sunday.gif' title='روز تعطیل است' border='0' alt=''>\r\n";
            break;
 
        case (3): // Fully booked day
            $txt= "<img src='images/block_fully_booked.gif' title='کامل رزرو شده' border='0' alt=''>\r\n";
            break;
 
        case (4): // Past day
            $txt = "<img src='images/block_past.gif' title='از تاریخ امروز گذشته' border='0' alt=''></a>\r\n";
            break;
 
        case (5): // Block booked out day
            $txt= "<img src='images/block_fully_booked.gif' title='این روز موجود نیست' border='0' alt=''>\r\n";
            break;
            
        default: // FREE
            $txt = "<a href='calendar.php?month=" .  $this->month . "&year=" .  $this->year . "&day=" . sprintf("%02s", $daynumber) . "'>
                    <img src='images/block_free.gif' title='آزاد برای رزرو' border='0' alt=''></a>\r\n";
            break;
    
        }
        
        return $txt;
   
    } // Close function
 
 
function make_key() {     
    
        // This key is displayed below the calendar to show what the colours represent
        
        echo "</tr></table>
        <table border='0' id='key' cellpadding='2' cellspacing='6'>
            <tr>
                <td id='key_1'> </td>
                <td id='key_2'> </td>
                <td id='key_3'> </td>
                <td id='key_4'> </td>
                <td id='key_5'> </td>
            </tr>
            <tr>
                <td>نبود وقت</td>
                <td>روز تعطیل</td>
                <td>حداقل یک وقت رزرو موجود است</td>
                <td>آزاد</td>
                <td>گذشته</td>
            </tr>                
        </table></div>"; 
        
        $this->make_booking_slots();  
             
} // Close function
 
        
function make_booking_slots() {
 
    /*
    Variable $day has a default value of 0.  If a day has been clicked on, display it.
    If there is no date selected, show a msg.  Otherwise show the booking form.
    */  
        
    if($this->day == 0) { 
        $this->select_day();   
    }  else {       
        $this->make_form();
    }  
    
    } // Close function  
 
 
function select_day() {
    echo "<form method='post' action=''><div id='selected_date'>لطفا روز رزرواسیون را انتخاب کنید</div>";    
}
 
function make_form() {
    
    // Create array of the booking times
    for($i = strtotime($this->booking_start_time); $i<= strtotime($this->booking_end_time); $i = $i + $this->booking_frequency * 60) {
        $slots[] = date("H:i:s", $i);  
    }
   
        echo "\r\n\r\n<form method='post' action=''><div id='selected_date'>تاریخ انتخاب شده: " . $this->month.",". $this->current_day.",". $this->year . "</div>";
 
         
    $opt = "<select id='select' name='booking_time'><option value='selectvalue'>لطفا زمان رزرواسیون را انتخاب کنید</option>";
    
        if($this->count >= 1) { 
          
            foreach($this->bookings as $row) {  
              
            // Check for bookings and remove any previously booked slots                 
                
            foreach($slots as $i => $r) { 
                    if($row['start'] == $r && $row['date'] == $this->year . '-' . $this->month . '-' . $this->day) {  
                    unset($slots[$i]); 
                    }          
                  
                    } // Close foreach
                                  
            } // Close foreach 
                       
        } // If count bookings                   
 
        
        // Make select box from $slots array
                       
        foreach($slots as $booking_time) {
            $finish_time = strtotime($booking_time)+ $this->booking_frequency * 60; // Calculate finish time
            $opt .= "<option value='" . $booking_time . "'>" . $booking_time . " - " . date("H:i:s", $finish_time) . "</option>";
        } 
    
        echo $opt. "</select>";          
 
        
        echo "
        <table width='300' border='0' id='booking'>
        <tr>
            <td class='details'>اسم و فامیلی</td>
            <td><input class='input' type='text' name='name' size='32'"; if(isset($_POST['name'])) echo " value='" . $_POST['name'] . "'"; echo "></td>
        </tr>
        <tr>
            <td class='details'>ایمیل</td>
            <td><input class='input' type='text' name='email' size='32'"; if(isset($_POST['email'])) echo " value='" . $_POST['email'] . "'"; echo "></td>
        </tr>
        <tr>
            <td class='details'>تلفن</td>
            <td><input class='input' type='text' name='phone' size='32'"; if(isset($_POST['phone'])) echo " value='" . $_POST['phone'] . "'"; echo "></td>
        </tr>
        <tr>
            <td class='details'>پیام</td>
            <td><textarea rows='3' cols='30' name='comments' onclick='make_blank();'>";
        if(isset($_POST['comments'])) echo $_POST['comments'];
        echo "</textarea></td>
        </tr>
        <tr>
            <td> </td>
            <td>
      <input type='submit' value='' id='book'></td>
        </tr>
        </table></form>";
}
 
 
function after_post($month, $day, $year) {
 
    $alert=''; $msg=0;
    
    if(isset($_POST['name']) && empty($_POST['name'])) { $msg = 1; $alert .= "Please fill in the name box<br>"; } 
    if(isset($_POST['email']) && empty($_POST['email'])) { $msg = 1; $alert .= "Please fill in the email box<br>"; }    
    if(isset($_POST['phone']) && empty($_POST['phone'])) { $msg = 1; $alert .= "Please add a contact number<br>"; }     
    if(isset($_POST['booking_time']) && $_POST['booking_time'] == 'selectvalue') { $msg = 1; $alert .= "Please select a booking time"; }
    
    if($msg == 1) {
    echo "<div class='error'>" . $alert . "</div>";    
    
    } elseif($msg == 0) {
 
            $booking_date = mds_date("Y-m-d", make_time(0, 0, 0, $month, $day, $year));
            $booking_time = $_POST['booking_time'];
            
            $query = "INSERT INTO bookings (date, start, name, email, phone, comments) VALUES ('$booking_date', '$booking_time',  '$_POST[name]', '$_POST', '$_POST[phone]', '$_POST[comments]')";
Edited by mrkhoros
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.