看似
相当标准的日期转换给了我不寻常的结果。我想将 $pay_date 转换为"j/d/Y"格式,但只有在格式为"Y-m-d"时才能获得正确的日期。
这是我的代码:
$payment = '2014-09-09';
$pay_date = strtotime("+1 month", strtotime($payment) );
$converter = date('j/d/Y', $pay_date );
echo $converter;
结果: 9/09/2014 (应为 10/09/2014)
如果我保留这些变量,但$converter更改为以下格式:
$converter = date('Y-m-d', $pay_date );
结果正确 - 2014-10-09
我也试过这个:
$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('j/d/Y');
echo $convert2;
结果: 9/09/2014
但:
$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('Y-m-d');
echo $convert2;
给我正确的结果: 2014-10-09
j
和d
是相同的格式选项,它们都表示DAY;来自PHP的date函数手册:
d Day of the month, 2 digits with leading zeros 01 to 31
j Day of the month without leading zeros 1 to 31
使用格式'm/d/Y'
.