How to insert a date into a database via the form instead of using the date("Y-m-d");

I have the following form and I am wondering if I can just enter all the information including the date in the form to get it submitted to the database without using the data(“Y-m-d”); at all? or must I use the date(“Y-m-d”) in my processing section?

<form class="signup-form" action="update_database_maintenance_level1_form.php" method="POST">
	<label>Enter Level 1 Maintenance Name:</label>
	<br /><br />
	<input type="text" name="name" placeholder="Enter Level 1 Maintenance Name">
	<br /><br />
	<label>Enter Level 1 Maintenance Description</label>
	<br /><br />
    <textarea name="describe" placeholder="Enter Level 1 Maintenance Description"></textarea>
    <br /><br />
    <label>Enter Level 1 Maintenance Date:</label>
    <br /><br />
    <input type="text" name="date" placeholder="Enter Level 1 Maintenance Date">
    <br /><br />
    <label>Enter Level 1 Maintenance Status:</label>
    <br /><br />
    <select name="status"> 
       <option value="" selected="selected">Enter Level 1 Maintenance Status:</option> 
       <option value="High">High Priority</option>
       <option value="Medium">Medium Priority</option>
       <option value="Low">Low Priority</option>
    <input type="hidden" name="csrf" value="<?php echo $_SESSION['key']; ?>">
    <button type="submit" name="submit">Update Level 1 Maintenance</button>

In my other page, I have defined all the variables and my date variable is $date = strip_tags($_POST[‘date’]); but when I entered the information, it just shows as…
0000-00-00 00:00:00

I have set my database as timestamp

Set the database date or created_on (or whatever it’s called) table as datetime and as default value put CURRENT_TIMESTAMP.


Edit: i always understand incorrect :weary:

Instead of blindly pulling everything through strip_tags and sending it to the database, I can highly recommend implementing some kind of validation system. So instead of a magical “fix any input” function, you decide on the specific data type which rules it should obey.

So for a timestamp, you could pull the (unfiltered) input through strtotime and see if it can be parsed as a valid date. That way, you can be sure that the input is a date, not just any random text.

1 Like

I guess I do have to use the date function either way… I just thought that I could insert the date… never mind, I am a bit confused here but will try to keep it simple by using the date function

actually @Ergastolator1 's advice is well easier :slight_smile:


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.