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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改