温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - How to check whether a date is in j-n-Y format or not? if yes then how to convert it to d-m-Y format
date php strtotime

php - 如何检查日期是否为jnY格式?

发布于 2020-03-27 10:19:23

我想查找日期是否为“ j / n / Y”格式,如果是,则要将ot转换为“ d / m / Y”。如果它已经是“ d / m / Y”格式,我不希望有任何更改。

我尝试了这个

         $date = '1/7/2019'; 

这里我假设1为日期,7为月,2019为年(显然)。现在,

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

上面的代码给我的输出为“ 07/01/2019”。但我想输出为“ 01/07/2019”。

并且如果日期已经是d / m / Y格式(例如17/12/2019),则当该字符串以上述日期转换代码传递时,它会给我输出“ 01/01/1970”。不知道为什么。

请帮忙!

查看更多

查看更多

提问者
KUSHAL AGRAWAL
被浏览
201
Manav 2019-07-03 21:21

一种方法可能是explode将日期变成其日月和年部分,然后对其进行分析

$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;