Separation of a php string

Asked

Viewed 105 times

2

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

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

and so insert:

$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);

pff someone can help me?

1 answer

3


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

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

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

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

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

Happy New Year. :)

  • Thank you! Happy New Year

  • Just fixing the friend @lffg, no explode change the bar by hyphen, getting $turno_separado = explode('-', $turno);.

  • Oops, it’s true. I already edited above. Thanks for the remark, Thiago. :)

  • I noticed but it wasn’t important

Browser other questions tagged

You are not signed in. Login or sign up in order to post.