How to check that time entry exist for every 10minutes

Sorry guys, Disturbing u all again…I need to do some validation for my application so this is important for me So i need some suggestion…
I am retrieving timestamp from database based on user input.i.e user enters the starting point and ending point…

Time is retrieved in the following format

 23-12-2015 11:10
 23-12-2015 11:20
 23-12-2015 11:30
 23-12-2015 11:40
 23-12-2015 11:50
 23-12-2015 12:00

And im storing the retrieved value in an array $time…Now the task is to check that the retrieved value contains timestamp every 10 minutes and if does not contain i need to insert that value and store in a array

Below is the Html Code

  	  <label id="from"  style="display:none;">Enter starting Date and Time</label>
		
	   <input id="dateInputone" name="dateipone" type="datetime-local" step="600" onblur="validate_time(this.value)" style="display:none;" >
	   
	   <label id="to"  style="display:none;">Enter Ending Date and Time</label>
	   
	   <input id="dateInputtwo" name="dateiptwo" type="datetime-local" step="600" style="display:none;">

               <input type="submit"  value="submit">

Below is my PHP Code

  $query = "SELECT timestamp FROM inverterlog where inverter = '" . $inverter . "' AND timestamp BETWEEN '" . $fromdate . "' AND '" . $todate . "'";
   $time=array();
  if ($output = mysqli_prepare($con, $query))
  {
   while(mysqli_stmt_fetch($output))
	   {
		   array_push($time, "$timestamp");
       }
  }

i do not see where your tried to walk through all values, check if the time has the right gap to the time before, and insert it if not found. why don’t you take the first timestamp and calculate the gaps until your endtime is reached? DatePeriod may help.

In this code

  $query = "SELECT timestamp FROM inverterlog where inverter = '" . $inverter . "' AND timestamp BETWEEN '" . $fromdate . "' AND '" . $todate . "'";
   $time=array();
  if ($output = mysqli_prepare($con, $query))
  {
   while(mysqli_stmt_fetch($output))
	   {
		   array_push($time, "$timestamp");
       }
  }

you don’t seem to be executing the query, or binding the results to your $timestamp variable.

@droopsnoot
Below is the complete code

 $query = "SELECT timestamp FROM inverterlog where inverter = '" . $inverter . "' AND timestamp 
  BETWEEN '" . $fromdate . "' AND '" . $todate . "'";
  if ($output = mysqli_prepare($con, $query))
   {
	   mysqli_stmt_execute($output);
	   mysqli_stmt_bind_result($output, $timestamp);
       $time=array();
       while(mysqli_stmt_fetch($output))
       {
	     array_push($time, "$timestamp");
       }
    foeach($time as $value)
     {
         //Now need check time interval is in sequence or not and if not insert the timestamp
     }
   }

Why don’t you do that while you’re running through the while() loop? Get the new timestamp, check to see whether it’s the correct gap from the previous one, if it isn’t insert some values to fill in the gaps, then insert the new timestamp, then carry on with the loop.

$last_time = "":
while(mysqli_stmt_fetch($output))
{
 // if (timestamp - last_time > required gap) and (last_time <> "") {
    // insert enough to fill the gap
    }
  array_push($time, "$timestamp");
  $last_time = $timestamp;
  }

@droopsnoot
Time gap should be 10min b/w previous and next value… i tried to use dateperiod …But failed in my attempt…

Below is my code

      $start    = DateTime::createFromFormat('m-d-Y H:i:s', $fromdate);
	 
     $end      = DateTime::createFromFormat('m-d-Y H:i:s', $todate);
	 
     $interval = new DateInterval('PT10M');
     $period   = new DatePeriod($start, $interval, $end);
     
    foreach()
   {


   }

I’d go something like:

$last_time = "":
$dtformat = "Y-m-d H:i:s";
$time = array();
while(mysqli_stmt_fetch($output))
{
  if ($last_time != "" ) { 
    $newtime = strtotime($timestamp);
    $interim = date("+10 minutes", $last_time);
    while ($interim < $newtime) {  // fill in the gaps, if there are any
      array_push(date($dtformat, $interim));
      $interim = date("+10 minutes", $interim);
      }
    }
  array_push($time, $timestamp);
  $last_time = strtotime($timestamp);
  }

ETA: actually, having read this bit again from the first post:

there will be additional work to do to convert the incoming value into the standard MySQL timestamp format. I’ll leave that bit to you. If it’s not too late, in my opinion it would be better to change your database to store these in the standard format, as you can do date / time comparisons using standard queries rather than having to manually deal with dates using your format.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.