Error when submitting form with two date fields and using date_diff() in PHP
P粉817354783
P粉817354783 2023-09-13 23:39:55
0
1
465

Hello everyone, I am submitting a form containing two fields "time_in" and "time_out"

I'm trying to get the difference between two dates using date_diff() I made sure that time_in in the database is in DATE formant and time_out in DATE format and both are not strings

I get this error: Fatal error: Uncaught type error: date_diff0: Parameter #1 (SbaseObject) must be of type DateTimeInterface, string in C:\ampp\htdocs\aicAdmin\scripts\backend-script.php: Trace given in 97Stack: #0 C:ilxampp\htdocs\aicAdmin\scripts\backend-script.php(97):date_diff(°2023-05-19,2023-05-23#1(main) throwinC:\xampp\htdocs\aicAdmin\scripts\backend -script.phponline97

And my code is

<div class="col">
            <div class="form-group">
                <input type="date"  class="form-control"  placeholder="Enter Time in" name="time_in" value="<?php echo $time_in; ?>">
            </div>
        </div> 

    <div class="col">
            <div class="form-group">
                <input type="date"  class="form-control"  placeholder="Enter Time out" name="time_out" value="<?php echo $time_in; ?>">
            </div>
        </div>

and backend code

if(empty($_GET['id']) && !empty($_GET['name']) && $_GET['name']=='current_job'){
   extract($_POST);
  if(!empty($jobnumber)){
   

      $data= [
        'jobnumber'=>$jobnumber,
       'revison' =>$revison,
       'groupp'=>$groupp,
       'checker'=>$checker,
       'releasedate'=> $releasedate,
       'quote_number'=> $quote_number,
       'building_number'=>$building_number,
       'task'=>$task,
       'designer_ca'=>$designer_ca,
       'designer_name'=>$designer_name,
       'time_in'=>$time_in,
       'time_schedule'=>$time_schedule,
       'time_out'=>$time_out,
       'spent_time'=>date_diff($time_out,$time_in),   
       'quote_weight'=>$quote_weight,
       'job_weight'=>$job_weight,
       'cycle_time'=>$cycle_time,
       'chk_time'=>$chk_time,
       'wd'=>$wd,
       'remarks'=>$remarks

       
     ];
  

    $tableName=$_GET['name']; 

    if(!empty($data) && !empty($tableName)){
       $insertData=insert_data($data,$tableName);
       if($insertData){
         echo "<span class='success'>Current Job Was saved sucessfully</span>";
       }else{
         echo "<span class='fail'>Error!.. check your query</span>";
       }
    }

}else{
  echo "<span class='fail'>Current Job field is empty</span>";
}

}

I did not copy the entire form code, just a snippet How can I solve this problem? ?

I double checked that time_in and time_out in the database are stored as DATE format, I hope everything is working properly

P粉817354783
P粉817354783

reply all(1)
P粉966979765

There isn't enough information here to really answer this question.

But from the error, the problem seems to be that your time_in and time_out are not DateTime objects.

You must first push them via DateTime:CreateFromFormat. This will make them DateTime objects. Then date_diff will give you a DateInterval object that still cannot be stored cleanly in the database.

Consider utilizing unix epoch timestamps.

$datetime_in = DateTime::createFromFormat('h:i a', $form_time_in); 
$datetime_out = DateTime::createFromFormat('h:i a', $form_time_out);
$seconds_elapsed = $datetime_out->format('U') - $datetime_in->format('U');

Note: "h:i a" requires the format "1:30 am", see other options here.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template