0

我的日期格式有问题,它是从 SQL 数据库获取的值并传递给用户的表单,作为回报,当用户将其设置回存储到数据库中时,格式已经不一样了。

$sql = mysql_query("SELECT * FROM $memberTable WHERE id='11' LIMIT 1"); //checking from SQL
$sqlcheck = mysql_fetch_assoc($sql); //Pass each value

$dob = strftime("%Y-%B-%d", strtotime($sqlcheck['dob']));
//format from databases 2000-10-30 into 2000-October-30

$dob = explode("-", $dob);
// break into day,month,year for form to fill in

$dob = $dob[0].'-'.$dob[1].'-'.$dob[2];
// after user fill in combine together for user to input back to databases

$dob = strftime("%Y-%m-%d", strtotime($dob));
//formatting back into databases format, 2000-October-30 into 2000-10-30
//The main problem here is the output is "2000-10-02"

我想知道为什么 day value pass 变成 02 而不是 30 ?我使用的格式代码有问题吗?请帮忙。

4

3 回答 3

3

尝试使用date类似的功能

echo $dob = date("Y-m-d", strtotime($dob));

并且更好地使用like(可选)

$dob = strtotime("Y-B-d", strtotime($sqlcheck['dob']));
//format from databases 2000-10-30 into 2000-October-30

$dob = explode("-", $dob);
// break into day,month,year for form to fill in

$dateofbirth = $dob[0].'-'.$dob[1].'-'.$dob[2];
echo date("Y-m-d", strtotime($dateofbirth));
于 2013-06-24T05:25:56.803 回答
1

@您只是没有为该strtotime()功能制作写入日期格式,您可以像这样正确地做到这一点:-

$date='2000-October-30';
$dob = explode("-", $date);
$dob = $dob[2].'-'.$dob[1].'-'.$dob[0];
echo $dob = strftime("%Y-%m-%d", strtotime($dob));

您可以在此处检查 php 日期和时间格式

于 2013-06-24T05:52:53.813 回答
0

strtotime 方法在格式为“”时正确计算日期,但在日期为“ ”2013-Oct-30格式时不计算2013-October-30

当您将日期重新构建在一起时,只需在该月上执行一个子字符串即可获取该月的前 3 个字符,这应该可以解决您的问题。

$dob = $dob[0].'-'. substr($dob[1], 0, 3) .'-'.$dob[2];

// 用户填写后组合在一起供用户输入回数据库

于 2013-06-24T06:21:58.747 回答