格式日期输出差异PHP


Format date output difference PHP

我的日期格式有问题,该格式是从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"

我想知道为什么日值通行证变成02而不是30?我使用的格式代码有问题吗?请帮忙。

尝试使用类似的date功能

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

更好地像(可选)一样使用

$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));

@如果不为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-Oct-30",strtotime方法将正确计算日期但当日期为"2013-October-30"格式时不计算

当你重新构建日期时,只需在这个月上做一个子串,就可以得到这个月的前3个字符,这应该可以解决你的问题。

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

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