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

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

我还没有找到一种简单的方法来执行此操作,并且很少有地方解决 2 年的日期格式。我读过 strtotime(),它实际上只能处理 4 位数年份或美国格式 - 这对我没有帮助。

最后,我通常会把字符串分解成一个数组,在年份前面加上 20,然后用那个 BUT 进行转换,这看起来真的很麻烦。

我为这个例子苦苦思索:http://php.net/manual/en/function.strtotime.php#100144 却无法让它工作...看起来很简单...然后我阅读了注释;

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.

是否有更经济的方法来处理这些日期,或者我是否永远被迫每次都执行 3-4 行转换?

RO 日期:19/04/18
RO 日期:18/06/18
RO 日期:19/06/18
RO 日期:19/06/18
RO 日期:2018 年 6 月 19 日

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

使用DateTime::createFromFormat():

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

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

结果:

1983-03-24

https://3v4l.org/KIVbM

如果年份是 1983,请使用 Y,所有格式请参见上面的手册 link。