Convert and save time to database with PHP

Asked

Viewed 421 times

0

I have an input field where the user type an hour (eg "08:00") convert this string with strtotime() and convert again with date() and saved in mysql in TIME format. So far, so good, although I think there’s a better way to do that. The problem is that if the input field is empty it is saved in the "00:00:00" bank and should not save anything. If you do an if (if($sfi1)) to know if strtotime returns false and use date() only if something exists, everything works correctly. But I want to know if you have a better way than using if in every input.

public function store(Request $request)
{
    $input = $request->all();
    $this->modality = new Modality($input);

    $sfi1 = strtotime($request->input('segunda-feira-inicio1'));
    if($sfi1) {
        $this->modality->segunda_feira_inicio1 = date("H:i:s", $sfi1);
    }
    $sff1 = strtotime($request->input('segunda-feira-fim1'));
    $this->modality->segunda_feira_fim1 = date("H:i:s", $sff1);

    $this->modality->save();
    return redirect()->action('ModalityController@index');
}

Form:

<div class="container">
<div class="panel panel-primary">
    <div class="panel-heading">Modalidades</div>
    <div class="panel-body">
        <form method="post" action="{{ route('store') }}" enctype="multipart/form-data">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="form-group">
                <label for="exampleInputEmail1">Nome</label>
                <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Modalidade" name="name">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">Descrição</label>
                <textarea class="form-control" rows="5" placeholder="Descrição" name="description"></textarea>
            </div>
            <div class="form-group">
                <label for="exampleInputFile">Upload de imagens</label>
                <input type="file" name="photo[]" multiple >
                <p class="help-block">Para selecionar multiplas imagens segure Crtl e escolha as imagens</p>
            </div>
            <div class="table-responsive">
                <label for="exampleInputFile">Horários</label>
                <table class="table">
                    <tr>
                        <th>Dia da Semana</th>
                        <th>Início 1</th>
                        <th>Fim 1</th>
                        <th>Início 2</th>
                        <th>Fim 2</th>
                        <th>Início 3</th>
                        <th>Fim 3</th>
                        <th>Início 4</th>
                        <th>Fim 4</th>
                    </tr>
                    <tr>
                        <td>Segunda-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="segunda-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Terça-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="terca-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Quarta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quarta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Quinta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="quinta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Sexta-feira</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sexta-feira-fim4">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>Sábado</td>         
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim1">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim2">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim3">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-inicio4">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="time" class="form-control" name="sabado-fim4">
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <button type="submit" class="btn btn-primary">Adicionar modalidade</button>
        </form>
    </div>
</div>

  • If the user does not inform the data, in the database will be NULL?

  • What you can do is create a repeat loop that checks all values, but for that you would need to know exactly what your HTML form looks like.

  • And what is the default for names? Only changes the value at the end? And is there any other field than those in the form?

  • Yes, I said there is, but like I said, you need to know how the form is doing. I am not saying that he is wrong, but it is necessary to know what is the name of the fields. Without the name, how will I know which fields should I check? Just say if the names follow a pattern or not or if there are other fields besides these in the form. Without that information, there’s no way to answer you. So if you want help, please help.

2 answers

2


With Laravel it is possible to get the list of all values passed in the request, except for some, through the method:

$request->except([...]);

So, first we start by storing this list in a variable:

$fields = $request->except(["_token", "name", "description", "photo"]);

To scroll through all values, simply use a loop:

foreach($fields as $field => $value) {
    // ...
}

If the field value is empty, refreshes the column to NULL:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        // ...
    }
}

We convert the time to the desired format:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }
}

Field names are in format segunda-feira-inicio1, but the column names are in the format segunda_feira_inicio1, that is, to get the respective column name from the field name just replace - for _:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }

    $column = str_replace('-', '_', $field);
}

And then we assign the value to the column:

foreach($fields as $field => $value) {
    if(empty($value)) {
        $value = NULL;
    } else {
        $column = str_replace('-', '_', $field);
        $value = date("H:i:s", strtotime($value));
    }

    $column = str_replace('-', '_', $field);
    $this->modality->{$column} = $value;
}

Thus, your complete method should look like:

public function store(Request $request)
{
    $input = $request->all();
    $this->modality = new Modality($input);

    $fields = $request->except(["_token", "name", "description", "photo"]);

    foreach($fields as $field => $value) {
        if(empty($value)) {
            $value = NULL;
        } else {
            $column = str_replace('-', '_', $field);
            $value = date("H:i:s", strtotime($value));
        }

        $column = str_replace('-', '_', $field);
        $this->modality->{$column} = $value;
    }

    $this->modality->save();
    return redirect()->action('ModalityController@index');
}

0

You can put the required HTML attribute in your HTML. Thus, the field will always be required.

Something like:

<input type="text" name="hora" required>

Thus, the request will not be made to PHP if it is empty. You can also use Javascript + HTML masks to validate the inserted time format, to prevent the user from entering anything in writing or something like that.

To validate the inputs, you can take the $input you received and go through it by validating if the value is equal to '' (blank), if it is, you null arrow if it is a 'time' input, with this, if the value is null, it will be saved as no value in the database. To convert to array the input has the method toArray()

  • I get it. You can take your input and go through them all with a for, if its value is equal to '' (blank), you can set the value to null, so when saving to the bank it will not save as 00:00:00

Browser other questions tagged

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