Problem with date and time

Hello,

i don’t success to add minutes to a given time… i do something wrong in my code but i don’t get what…

i get the duration from a “first step” form, and then i want to add it to my start_time session… but it gives me strange time and not the correct end_test_time…

I think the proble is in those lines :

$end_time_session = $start_time_session + $duration_test;
$end_test_time = $start_time_session + strtotime($duration_test);

See the full code here:
Thank your verry much for help !

<?php

$tests = Test::find()->all();

$price = 0;
$slots = 0;
$duration_test = 0;

foreach ($model->type as $testid) {  
            
    foreach ($tests as $test) {
                
        if($test->id == $testid){
        	echo $test->brand.' - ';
        	echo $test->type.'<br>';
            $duration_test = $duration_test + (int)$test->duration; //duration of the test
            $slots = $slots + (int)$test->slot_time; //time slots (ex: X * 15min)
            $price = $price + (int)$test->price; //price of the test
                    
            if($price > 220){
                $price = 220;
            }
        }

    }

}
 
$today = date('Y-m-d',strtotime("+10 days"));
//echo $today;
echo 'Duration: '.$duration_test.' min<br>';
echo 'Slots: '.$slots.'<br>';
echo 'Price: CHF '.$price.'.-<br>';      
echo 'Day: '.date("d M Y", strtotime($today)).'<br><br><br>';
    
$sessionsInside = Session::find()->where(['>=', 'start_date', $today])->orderby(['start_date'=>'ASC'])->all(); //session ex: 2018-09-09 9h00 to 11h45
$inscriptionsInside = Inscription::find()->where(['>=', 'day', $today])->orderby(['day'=>'ASC'])->all();

foreach ($sessionsInside as $session) {

    $array_of_time = [];
    $endarray = [];
    $session_id = $session->id;
    $session_start_day = $session->start_date; //  day of session 
    $session_start_time    = strtotime ($session->start_time); //start time of sesion 9h00
    $session_end_time      = strtotime ($session->end_time);  //end time of session 11h45
    //$session_start_daytime =  strtotime( $session_start_day.' '.date('H:i:s', $session_start_time) );
    

    $fifteen_mins  = 15 * 60;

    while ($session_start_time <= $session_end_time) {
        
        $array_of_time[] = $session_start_time;      

        $session_start_time += $fifteen_mins;

    }


    foreach ($array_of_time as $start_time_session) {

        $place_count = 2;

        $session_start_daytime =  $session_start_day.' '.date('H:i:s', $start_time_session);
       
        $end_time_session = $start_time_session + $duration_test;
        $end_test_time = $start_time_session + strtotime($duration_test);
       

        foreach ($inscriptionsInside as $inscription) {
            //echo ' dd '.$inscription->start_time.' dd -';
            if($inscription->start_time == $session_start_daytime){
              
                //echo 'X '.$inscription->start_time.'<br>';
                $place_count--;
            }

            elseif($inscription->start_time > $session_start_daytime){
                if($inscription->start_time < $end_time_session){
                  
                    $place_count--;
                }
            }
            elseif($inscription->start_time < $session_start_daytime){
                if($inscription->end_time > $session_start_daytime){
                  
                    $place_count--;
                }

            }

        }
        //echo $place_count.' - ';
        if($place_count > 0) {
             //echo $session_start_daytime.' >>> '.$end_time_session.'<br>'; 
             $endarray[] = $session_start_daytime.' >>> '.$end_time_session;  


        }
        
    }

    echo '<br>'.$session_start_day.'<br><br>';
    echo 'PLACES : '.$place_count.'<br>';

    foreach ($endarray as $timing) {

        echo 'timing: '.$timing.'<br>';
        # code...
    }
    

}
?>

maybe this is an option for you: https://packagist.org/packages/nesbot/carbon

Hello, thank’s for your proposition. I unfortunately dont success to use Carbon in my yii2 … :confused:

What is not working?