douwa0280 2017-10-30 12:22
浏览 10
已采纳

PHP以错误的格式捕获日期

I know there is probably a simple answer to this but I have tried several things and nothing seems to work.

I have the following data taken from a JSON array:

{
"Id": 21973679,
"InvoiceNumber": 13,
"IssueDate": "14/08/2017",
"EulerID": 6162023,
"CustomerName": "Alderley plc",
"TotalAmount": "1,704.00",
"TotalTaxAmount": 0,
"Currency": "USD",
"DueDate": "13/09/2017",
"AmountDue": "0",
"Status": "Paid"
},
{
"Id": 21974783,
"InvoiceNumber": 23,
"IssueDate": "09/01/2017",
"EulerID": 6162023,
"CustomerName": "Alderley plc",
"TotalAmount": "11,000.00",
"TotalTaxAmount": 0,
"Currency": "GBP",
"DueDate": "08/02/2017",
"AmountDue": "0",
"Status": "Paid"
}, 

I am trying to get the IssueDate and DueDate. Both are stored in the format dd/mm/yyyy. However whatever I do php seems to not be able to capture them consistently.

$report_starting_date=$item1['IssueDate'];

$report_starting_date=date('d/m/Y',strtotime($report_starting_date));

For the 2nd Item invoice (23) it works fine if I echo the month it will return 1 this seems to work as the date could be either dd/mm or mm/dd however in the 1st item invoice (13) 14 cannot be a month and so php seems to set the date as 01/01/1970. I have no idea why I can store one item correct and not the other?

Thanks

  • 写回答

4条回答 默认 最新

  • douseda0009 2017-10-30 12:28
    关注

    Per the PHP documentation on supported formats, dd/mm/YYYY is not supported for strtotime.

    You could use the createFromFormat function, and the documentation is here.

    <?php
    $date = date_create_from_format('d/m/Y', $item1['IssueDate']);
    echo date_format($date, 'Y-m-d');
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题