Balaji
Balaji

Reputation: 45

Form date not matched when storing to MySQL using PHP

I have created an event page that shows the current date and time for registration. But day and month are changed when I used to store the registration date in the MySQL database.

event.php

<form name="eventForm" action="event-process.php" method="POST">
<input type="text" name="registraionDate" id="datepicker" value="<?php print date('m-d-Y H:i'); ?>">
</form>

Date Shown in form - August, 09
08-09-2021 14:14

event-process.php

$registraionDate= $_POST['registraionDate'];
print date('Y-m-d H:i:s', strtotime($registraionDate));

Output Date - September, 08
2021-09-08 14:14:00

Upvotes: 0

Views: 41

Answers (2)

Ilia Yatsenko
Ilia Yatsenko

Reputation: 832

Please see the formats accepted by strtotime: https://www.php.net/manual/ru/datetime.formats.date.php

You should apply one of them to your date in form, in order to be possible to parse it back using strtotime.

Alternatively, if you want to show date in custom format, you can use date_create_from_format in your event-process.php, like this:

$registraionDate= $_POST['registraionDate'];
print date_create_from_format('m-d-Y H:i', $registraionDate)->format('Y-m-d H:i:s');

Upvotes: 1

Tykok
Tykok

Reputation: 51

You simply inverted the 'm' and the 'd' in the format of your date in the "processus-événement.php" file

Upvotes: 0

Related Questions