doujiao2443 2016-06-03 15:05
浏览 53
已采纳

使用strtotime转换时的php日期问题,但如何正确地执行此操作?

Using the following code to try and get 'Y-m-d', and should return 2016-05-10, but it is instead returning 2016-10-05.

// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';

// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));


echo $convert_date;

How do I get 'Y-m-d' returned? Not trying to use explode('-', $test_date). Is this possible to do using proper time functions?

  • 写回答

3条回答 默认 最新

  • doupi6737 2016-06-03 15:08
    关注

    Yes, use DateTime object for this:

    $test_date = '05-10-2016';
    $DateTime = DateTime::createFromFormat('m-d-Y', $test_date, new DateTimeZone('utc'));
    var_dump($DateTime);
    

    OUTPUT

    object(DateTime)[8]
      public 'date' => string '2016-05-10 15:08:53.000000' (length=26)
      public 'timezone_type' => int 2
      public 'timezone' => string 'UTC' (length=3)
    

    So

    echo $DateTime->format('Y-m-d'); //2016-05-10 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?