2013-06-24 137 views
0

我有我的日期格式的问题,这是从SQL数据库获取的值并传递给用户的窗体,并在用户将其设置回存储到数据库格式不同时返回的值。格式日期输出差异PHP

$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? 是我用的格式代码有问题吗? 请帮助。

+0

仅仅因为你可以建立'strftime'的格式并不意味着'strtotime'能理解它。 – Gumbo

+0

将2000-10-30转换为2000年10月30日之后,可以使用strftime(“%Y-%m-%d”,strtotime($ dob));.问题在这里。对。建议不要转换为2000年10月30日。然后,直接使用strftime();将显示正确的结果。 –

回答

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日期和时间格式here

+0

Thx为您的答案,我现在知道我的问题。 –

+0

@DiazLv您好! –

3

尝试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)); 
0

的的strtotime方法正确地计算日期格式是否为“2013-Oct-30” 但是当日期的格式为“2013-October-30

当你正在构建的日期重新走到一起只是没有计算它在本月做一个子字符串以获取本月的前三个字符,并且应该解决您的问题。

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

//用户填写后,在共同为用户返回相结合,输入到数据库