douchujian8124 2019-07-03 13:14 采纳率: 100%
浏览 216
已采纳

如何检查日期是否为j-n-Y格式? 如果是,那么如何将其转换为d-m-Y格式? [重复]

This question already has an answer here:

I want to find whether a date is in "j/n/Y" format or not, if its is then i want to convert ot to "d/m/Y". If it is already in "d/m/Y" format i dont want any alteration in it.

I tried this-

         $date = '1/7/2019'; 

here i am assuming 1 as date, 7 as month, and 2019 as year(obviously). Now,

        $date_format_changed = date("d/m/Y", strtotime($date));

this above code gives me output as "07/01/2019". But i want output as "01/07/2019".

and if date is already in d/m/Y format eg- 17/12/2019, when this string will be passed in above date conversion code, it gives me output "01/01/1970". Dont know why.

Please help!

</div>
  • 写回答

2条回答 默认 最新

  • dongyu1983 2019-07-03 13:21
    关注

    An approach could be to explode the date into its day month and year components, and then analyse it

    $date = '1/7/2019'; 
    
    $array = explode("/", $date);
    
    $day = $array[0];
    $month = $array[1];
    $year = $array[2];
    
    if(strlen($day) == 1) $day = "0".$day;
    if(strlen($month) == 1) $month = "0".$month;
    
    echo $day."/".$month."/".$year;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器