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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀