我的日期格式有问题,这是从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值变成了02而不是30?我使用的格式代码有问题吗?请帮帮忙。
发布于 2013-06-24 13:52:54
@你只是没有为strtotime()函数写日期格式,你可以像这样做:-
$date='2000-October-30';
$dob = explode("-", $date);
$dob = $dob[2].'-'.$dob[1].'-'.$dob[0];
echo $dob = strftime("%Y-%m-%d", strtotime($dob));你可以检查php的日期和时间格式here
发布于 2013-06-24 13:25:57
尝试使用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-24 14:21:59
如果格式为"2013-Oct-30“,则strtotime方法将正确计算日期,但当日期格式为"2013-October-30”时,该方法不会计算日期。
当您重新构建日期时,只需在月份上执行一个子字符串以获取月份的前3个字符,这应该可以解决您的问题。
$dob = $dob[0].'-'. substr($dob[1], 0, 3) .'-'.$dob[2];//用户填写后组合在一起,用户输入回库
https://stackoverflow.com/questions/17268516
复制相似问题