How to use Timepicker in PHP Example?

Apr 04, 2022 . Admin



Hello dev,

I am going to explain you How to use Timepicker in PHP Example. You will learn How to use time picker in php. In side this article we will see How To Bootstrap Add Time Picker to Input Field.

This article will give you simple example of How To timepicker php mysql Code. We will use get simple How To Use Bootstrap timepicker in PHP & MySQL.

I will give you simple Example of How to use Timepicker in PHP Example.

So, let's see bellow solution:

connection.php
<?php 
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "aatman";

    $conn = new mysqli($servername,$username,$password,$dbname);

    if($conn->connect_error){
        die ('connection faild:'.$conn->connect_error);
    }
?>
index.php
<?php
    session_start();
    if(isset($_SESSION["msg"]) && !empty($_SESSION["msg"]))
        {
            $msg=$_SESSION["msg"];
            echo $msg;
            unset($_SESSION['msg']);
            session_destroy();
        }
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <title>How to use Timepicker in PHP Example</title>
    <style type="text/css">
        .error
        {
            color: red;
        }
    </style>
</head>
<body class="bg-dark">
    <div class="container mt-5">
        <?php 

            $error = $_SESSION['errors'];
            extract($error);
            session_destroy();

        ?>
        <div class="card">
            <div class="card-header">
                <h1 class="text-center">How to use Timepicker in PHP Example</h1>
            </div>
            <div class="card-body">
                <form action="process.php" method="post">
                    
                    <div class="row my-2">
                        <div class="col-md-6">
                            <label for="fname">First Name :</label>
                            <span class="error">*</span>
                            <input type="text" name="first_name" id="first_name" class="form-control" placeholder="Please Enter First Name">
                            <span class="error"><?php echo $first_nameErr;?></span>
                        </div>
                        <div class="col-md-6">
                            <label for="lname">Last Name :</label>
                            <span class="error">*</span>
                            <input type="text" name="last_name" id="last_name" class="form-control" placeholder="Please Enter Last Name">
                            <span class="error"><?php echo $last_nameErr; ?></span>
                        </div>
                    </div>
                    
                    <div class="row my-2">
                        <div class="col-md-6">
                            <label for="email">Email :</label>
                            <input type="email" name="email" id="email" class="form-control" placeholder="Please Enter email">
                        </div>
                        <div class="col-md-6">
                            <label for="ragistration_time">Ragistration Time :</label>
                            <span class="error">*</span><br>
                            <input type="time" name="ragistration_time" id="ragistration_time" class="form-control">
                            <span class="error"><?php echo $ragistration_timeErr;?></span>
                        </div>
                    </div>
                    
                    <div class="row my-2">
                        <div class="col-md-12">
                            <label for="address">Address :</label>
                            <span class="error">*</span><br>
                            <textarea name="address" id="address" rows="6" cols="40" class="form-control" placeholder="Please Enter address"></textarea>
                            <span class="error"><?php echo $addressErr;?></span>
                        </div>
                    </div>
            </div>

            <div class="card-footer text-center">
                <input type="submit" name="save" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>
</body>
</html>
process.php
<?php

    session_start();

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    extract($_POST); 
    $error = array();

        if (empty($first_name)) {
            $error['first_nameErr'] = "first name is required!";
        }
        if (empty($last_name)) {
            $error['last_nameErr'] = "last name is required!";
        }
        if (empty($ragistration_time)) {
            $error['ragistration_timeErr'] = "ragistration time is required!";
        }

        if (!empty($error)) {
            $_SESSION['errors'] = $error;
            header('location:index.php');
        }
        if (empty($error)){
            
            include "connection.php";
            
            $sql = "INSERT INTO students (first_name,last_name,email,address,ragistration)
                    VALUES('$first_name','$last_name','$email','$address','$ragistration_time')"; 

            if ($conn->query($sql)===TRUE) {
                $_SESSION['msg'] = "<div class='alert alert-success' role='alert'>Data Insert Successfully</div>";
                header('location:index.php');
            }else{
                $_SESSION['msg'] = "<div class='alert alert-danger' role='alert'>Error: ".$sql."<br>".$conn->error."</div>";
                header('location:index.php');
            }

            $conn->close();

        }else{
            header('location:index.php');
        }
    }
?>
Output:

It will help you...
#PHP