Jump to content

PHP / MySQL Date/Day


Carl

Recommended Posts

Hello,

 

I am having a little trouble getting the date/day to stick to a php form so when access'd the date and day of that form will show.

 

I have created a php form for storing pickup and drop off bookings for a transport company, for each day a new blank form and table is created, but I need to be able to enter the date on the current days form, and store it to that days mysql table, so the date and day is displayed on my php form. can anyone assist with this dilema?

 

 

Link to comment
https://forums.phpfreaks.com/topic/166487-php-mysql-dateday/
Share on other sites

Well, say you have something like this in a form:

<label for="pickupMonth">Month:</label>
<select name="pickup[month]" id="pickupMonth">
<option value="1">January</option>
<option value="2">February</option>
<!-- etc. -->
<option value="12">December</option>
</select>

<label for="pickupDay">Day:</label>
<select name="pickup[day]" id="pickupDay">
<option value="1">1</option>
<option value="2">2</option>
<!-- etc. -->
<option value="31">31</option>
</select>

<label for="pickupYear">Year:</label>
<input type="text" name="pickup[year]" id="pickupYear">

 

Then you can do like this:

if (!isset($_POST['pickup']['month']) || !isset($_POST['pickup']['day']) || !isset($_POST['pickup']['year'])) {
echo 'please complete all fields';
}
else if (!checkdate((int) $_POST['pickup']['month'], (int) $_POST['pickup']['day'], (int) $_POST['pickup']['year'])) {
echo 'please enter a valid date';
}
else {
$mysqlDate = sprintf('%04d-%02d-%02d', $_POST['pickup']['year'], $_POST['pickup']['month'], $_POST['pickup']['day']);
}

 

To validate the date and make it fit a MySQL DATE field, which is stored in ISO 8901 format (that is YYYY-MM-DD).

Link to comment
https://forums.phpfreaks.com/topic/166487-php-mysql-dateday/#findComment-877944
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.