1

I have a table to store talent leave its structure is like this

table:talent_leave

id   talent   from_date    to_date
 1    10    2016-09-02  2016-09-04

This entry means that He applied for a leave for 2016-09-02 to 2016-09-04. What I need is to display this days status when he try to apply another from and to date which has one of these dates[2016-09-02 , 2016-09-03 , 2016-09-04]

Eg:If he apply 2016-09-04 to 2016-09-06

My Application Should show

 2016-09-04 / Planned Leave
 2016-09-05 / Not Planned
 2016-09-06 / Not Planned

I have a solution in PHP but it is very slow The solution i Found is

$plannedLeaveArray=array();
$sql="select from_date and to_date From talent_leave Where talent=10";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
    $from_date=$row['from_date'];
    $to_date=$row['to_date'];
    while (strtotime($to_date) >= strtotime($from_date)) {
                $plannedLeaveArray[] = $from_date;
                $from_date = strtotime("+1 day", strtotime($from_date));
            }
}

Now i get the planned date in array

$from_post=$_POST['from'];
$to_post=$_POST['to'];
 while (strtotime($to_post) >= strtotime($from_post)) {
                if (in_array($from_post, $plannedLeaveArray)) {
                      echo $from_post."/".Planned Leave
                 }else{
                      echo $from_post."/".Not Planned
                 }
                $from_post= strtotime("+1 day", strtotime($from_post));
            }

It takes too much time to load.If you people have a better solution or suggestion that would be great.

Sanooj T
  • 1,317
  • 1
  • 14
  • 25
  • check `date between` – Saurabh Aug 12 '16 at 07:00
  • You want to stop the user from applying for another leave if the new dates are between once already in the table ? – Vasil Rashkov Aug 12 '16 at 07:00
  • stop the user and display the dates with status – Sanooj T Aug 12 '16 at 07:00
  • 1
    `$plannedLeaveArray = array(); $sql = "select from_date and to_date From talent_leave Where talent=10"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ // USE METHODS OF DATE RELATED CLASSES FOR EFFICIENCY... $from_date = new DateTime($row['from_date']); $to_date = new DateTime($row['to_date']); // IF $to_date IS GREATER THAN OR EQUAL TO $from_date // THEN WE KNOW WE HAVE A VALID LEAVE-DATE (+) // AND THUS ADD AN EXTRA DAY TO THE $from_date VALUE` – Poiz Aug 12 '16 at 07:22
  • 1
    `// AND PUSH IT TO THE $plannedLeaveArray THROUGH EACH ITERATION.... if($to_date->getTimestamp() >= $from_date->getTimestamp()) { // ADD ONE DAY TO THE $from_date AND PUSH IT TO $plannedLeaveArray. $from_date = $from_date->add(new DateInterval("P1D")) ->getTimestamp(); $plannedLeaveArray[] = $from_date; } }` – Poiz Aug 12 '16 at 07:23

0 Answers0