从HTML表单将字符串转换为日期

时间:2015-06-19 12:57:22

标签: php date converter

我收集这样的日期格式:

<select id="DOBDay" name = "DOBDay" class="pure-input-1-4">
<option>Date Of Birth Day</option>
<option value="01">01</option>

<select id="DOBMonth" name = "DOBMonth" class="pure-input-1-4">
<option>Date Of Birth Month</option>
<option value="06">06</option>

<select id="DOBYear" name = "DOBYear" class="pure-input-1-4">
<option>Date Of Birth Year</option>
<option value="2000">2000</option>

表单发布后,收集如下:

$DOBDay = $_POST['DOBDay'];
$DOBMonth = $_POST['DOBMonth'];
$DOBYear = $_POST['DOBYear'];

如何将其转换为日期格式以存储到MySQL日期中。 我在网上搜索过,无法在任何地方找到这个例子以获得帮助。

4 个答案:

答案 0 :(得分:1)

试试这个:

$DOBDay = '21';
$DOBMonth = '09';
$DOBYear = '1986';  
$full_date = $DOBYear.'-'.$DOBDay.'-'.$DOBMonth;
echo $full_date;`

答案 1 :(得分:0)

很多可能性......例如mktime。

// if you need a timestamp
$timestamp = mktime(0,0,0,$DOBMonth, $DOBDay, $DOBYear);

// or datetime
$datetime = date("Y-m-d", $timestamp);

答案 2 :(得分:0)

mysql使用yyyy-mm-dd格式。所以你应该将值存储在以下内容中:

$DOBDay = '21';
$DOBMonth = '09';
$DOBYear = '1986';  
$date = $DOBYear.'-'.$DOBMonth.'-'.$DOBDay;
echo $date;

答案 3 :(得分:-1)

$date= $_POST['DOBDay']."/". $_POST['DOBMonth']."/".$_POST['DOBYear'];