0

What seemed to be a fairly standard date conversion is giving me unusual results. I want to get $pay_date into the 'j/d/Y' format, but can only get the correct date if the format is 'Y-m-d'.

Here's my code:

$payment = '2014-09-09';
$pay_date = strtotime("+1 month", strtotime($payment) );                    
$converter = date('j/d/Y', $pay_date );

echo $converter;

result: 9/09/2014 (should be 10/09/2014)

If I keep these variables, but change $converter to this format:

$converter = date('Y-m-d', $pay_date );

the result is correct - 2014-10-09

I also tried this:

$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('j/d/Y');
echo $convert2;

result: 9/09/2014

but:

$convert2 = DateTime::createFromFormat('Y-m-d', $pay_date)->format('Y-m-d');
echo $convert2;

gives me the correct result: 2014-10-09

4

1 回答 1

1

j并且d是相同的格式选项,它们都代表 DAY;来自PHP 日期函数的手册

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'

于 2013-09-23T23:19:21.047 回答