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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法