douqiang4245 2013-09-23 23:14
浏览 29
已采纳

在php中转换日期会根据日期格式返回不同的结果

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

  • 写回答

1条回答 默认 最新

  • doukeng7426 2013-09-23 23:19
    关注

    j and d are the same formatting options, they both represent DAYs; from the manual for PHP's date function:

    d  Day of the month, 2 digits with leading zeros  01 to 31
    j  Day of the month without leading zeros         1 to 31
    

    Use the format 'm/d/Y'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line