du5739 2018-07-19 04:20
浏览 96
已采纳

在php中将dd / mm / yy转换为日期的最简单和最简单的方法

I am yet to find a simple way to do this and very few places address date formats with 2 years. I have read strtotime() and it really only handles 4 digit years or American format - which doesn't help me.

In the end, I generally end up breaking the string into an array, adding 20 in front of year and converting with that BUT that just seems really cumbersome.

I toiled with this example: http://php.net/manual/en/function.strtotime.php#100144 and couldn't get it to work... It seemed so simple... then I read the notes;

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.

Is there a more economical way of processing these dates or will I forever be forced to perform 3-4 lines of conversion every time?

RO Date: 19/04/18
RO Date: 18/06/18
RO Date: 19/06/18
RO Date: 19/06/18
RO Date: 19/06/18

  • 写回答

1条回答 默认 最新

  • dsz90288 2018-07-19 04:30
    关注

    Simplest and easiest way to convert dd/mm/yy to date in php?

    Use DateTime::createFromFormat():

    <?php
    $date = '24/03/83';
    //
    echo date_create_from_format('d/m/y', $date)->format('Y-m-d');
    

    Or

    <?php
    $date = '24/03/83';
    //
    echo DateTime::createFromFormat('d/m/y', $date)->format('Y-m-d');
    

    Result:

    1983-03-24
    

    https://3v4l.org/KIVbM

    Use Y if year is 1983, see manual link above for all formats.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效