将日期时间格式更改为Y-m-d,但日期时间类似于17年7月12日下午6:20:39 [英] Change datetime format to Y-m-d, but the datetime is like 7/12/17 6:20:39 pm

查看:140
本文介绍了将日期时间格式更改为Y-m-d,但日期时间类似于17年7月12日下午6:20:39的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

我需要将此日期时间更改为 Ymd H:i:s 格式,从 dd / mm / yy H:i:s 格式开始。

I need to change this datetime into Y-m-d H:i:s format from dd/mm/yy H:i:s format.

这用于PHP或CodeIgniter项目。

This is for PHP or CodeIgniter project.

我从其他报告中得到了一些格式不正确的数据。

I got some data from other report not formatted well.

datetime
7/12/17 12:15:23 pm (dd/mm/yy H:i:s) format
8/12/17 5:18:12 am
20/12/17 5:12:24 pm
21/12/17 12:17:37 pm

如果我使用第一种格式日期作为示例。

If I get example using 1st format date.

$example_date = "7/12/17 12:15:23 pm"; // d F Y : 07-December-2017 12:15:23
$format_date = $date("Y-m-d", strtotime($example_date));

打印: 2007-12-17 12:15:23 这是错误的结果。

Its printed : 2007-12-17 12:15:23 It's a wrong result.

d财政年度日期将其打印为 2007年12月17日,但应为 2017年12月7日

In d F Y date it's printed 17 December 2007 but It's should be 07 December 2017.

通常我们可以这样更改日期:

Normally we can change date using like this :

$example_datetime = "13/12/2017 12:08:16 pm"; // (dd/mm/yy H:i:s) format
$format_date = date("Y-m-d H:i:s", strtotime($example_datetime));




echo $ format_date => 2017-12-13 12:08:16 是正确的。如果那是2017年,而这正是我想要的。

Result from echo $format_date => 2017-12-13 12:08:16 is correct. If the year is 2017 and this is what I wanted to.


推荐答案

DateTime createFromFormat 方法-指定输入日期的格式,然后格式化输出的期望格式-例如:

You could use the DateTime and createFromFormat method - specifying the format of the input date and then formatting how you wish for output - ex:

$example_datetime = "13/12/2017 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/Y H:i:s a', $example_datetime );

$output=$date->format('Y-m-d H:i:s');
echo $output; //  -> 2017-12-13 12:08:16






对于输入日期戳中的两位数年份,应该匹配小写的 y 而不是大写的 Y

$example_datetime = "13/12/17 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/y H:i:s a', $example_datetime );

$output=$date->format('Y-m-d H:i:s');
echo $output; //  -> 2017-12-13 12:08:16

这篇关于将日期时间格式更改为Y-m-d,但日期时间类似于17年7月12日下午6:20:39的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆