首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将dd/mm/yy转换为php中日期的最简单和最简单的方法

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

Stack Overflow用户
提问于 2018-07-19 04:20:52
回答 1查看 1.1K关注 0票数 0

我还没有找到一个简单的方法来做到这一点,很少有地方,地址日期格式与2年。我读过strtotime(),它实际上只处理4位数的年份或美国格式--这对我没有帮助。

最后,我通常会将字符串分解成一个数组,在一年前添加20,然后用它进行转换,但这看起来真的很麻烦。

我辛辛苦苦地做着这个例子: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行的转换每次?

日期: 19/04/18

日期: 18/06/18

日期: 19/06/18

日期: 19/06/18

日期: 19/06/18

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-19 04:30:49

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

使用日期::createFromFormat()

代码语言:javascript
复制
<?php
$date = '24/03/83';
//
echo date_create_from_format('d/m/y', $date)->format('Y-m-d');

代码语言:javascript
复制
<?php
$date = '24/03/83';
//
echo DateTime::createFromFormat('d/m/y', $date)->format('Y-m-d');

结果:

代码语言:javascript
复制
1983-03-24

https://3v4l.org/KIVbM

如果年份是Y,请使用1983,所有格式见上面的手动链接。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51414304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档