Separation of a php string

2

This is the following I have a variable in php that is the shift that this way (15: 00-16: 00) which represents the hours what I want is to take that variable and insert it into the database separately , that is, get in:

$variavel="15:00-16:00";

and insert like this:

$variavelhora1="15:00";
$variavelhora2="16:00";

Code:

$inserir_turno = nl2br(addslashes($_POST['inserir_turno']));

$query = sprintf("INSERT INTO trabalho (turno,variavelhora1,variavelhora2) values ('%s','%s','%s')", $inserir_turno, $variavelhora1, $variavelhora2);
        $pv = mysql_query($query);

Can anyone help me?

    
asked by anonymous 31.12.2017 / 17:01

1 answer

3

You can use the explode to transform the string into an array, like this:

 $turno = '15:00-19:00';

 $turno_separado = explode('-', $turno);

 $hora_inicio = $turno_separado[0]; // 15:00
 $hora_final  = $turno_separado[1]; // 19:00

We turned the shift into an array, using the explode() method PHP, based on hyphen.
This way, we can separate the two hours through the hyphen that separates them in a string.

Happy new year. :)

    
31.12.2017 / 17:37