skip to Main Content

hi everyone im submitting a form having two fields "time_in" and "time_out"

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

and i get this error : Fatal error: Uncaught TypeError: date_diff0: Argument #1 (SbaseObject) must be of type DateTimelnterface, string given in C:ampphtdocsaicAdminscriptsbackend-script.php:97Stack trace: #0
C:ilxampphtdocsaicAdminscriptsbackend-script.php(97):date_diff(°2023-05-19,2023-05-23#1(main]throwninC:xampphtdocsaicAdminscriptsbackend-script.phponline97

while 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 in the 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 didn’t copy the whole form code just a snippet
how can i fix this please ??

i double checked that in the database time_in and time_out are stored as DATE format and i excpect everything to work normally

2

Answers


  1. Their is not really enough info here to truly answer this quesiton.

    But from the error the issue appears to be your time_in and time_out are not DateTime object.

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

    Consider instead taking advantage of 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’ expects format "1:30 am" see other options here.

    Login or Signup to reply.
  2. I think a little more context may be needed to fully answer your question but, I’m going to assume you’re experiencing a similar problem to what I did last year with this same function.

    To test this out, I’d recommend trying to echo your date variables. If it works then they are the wrong data type as for the diff method to work the parameters needs to be Objects not Strings.

    Here are two ways you can get the data type your need.

    $start_date= new DateTimeImmutable('2009-10-11');
    $end_date= new DateTimeImmutable('2009-10-13');
    $interval = $origin->diff($target);
    echo $interval->format('%R%a days');
    

    Alternatively you can do it this way.

    $time_out= new DateTime('today');
    $time_in= new DateTime('tomorrow');
    $interval = date_diff($time_out, $time_in);
    echo $interval->format('%R%a days');
    

    Also, I recommend documentation here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search