[英]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));
最好使用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));
@您只需不为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];
//用户填写后,将其组合在一起以供用户输入回数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.