如何在PHP中将标准AS400 CYMD日期格式转换为MDY? [英] How can I convert the standard AS400 CYMD date format to MDY in PHP?

查看:153
本文介绍了如何在PHP中将标准AS400 CYMD日期格式转换为MDY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP中进行很多查询.我正在显示来自这些查询的信息,但我想将日期格式更改为程序员以外的其他人可以轻松阅读的格式.目前,我已经可以通过使用以下方法来获得帮助:

I am doing quite a few queries within PHP. I am displaying information from these queries but I would like to change the date format to something that people other than programmers can easily read. Currently I have been able to get by by using this:

$compdt = str_split($fin47['COMPDT']);
$compdt = "$compdt[3]$compdt[4]/$compdt[5]$compdt[6]/$compdt[1]$compdt[2]"

以上效果很好.例如,从数据库中,我将其返回为日期:

The above works great. For example, from the database, I return this for the date:

1090225

在我完成数组和字符串的排列后,我得到了:

After I do my array and string arrangement, I get this:

02/25/09

但是当我从数据库中返回一个2000年前的日期时,是这样的:

But when I have a pre 2000 date returned from the database like this:

960614

我在安排好字符串后得到了这个

I get this after my string arrangement:

61/4/60

这显然是不正确的,因为世纪数字不存在,年份也为零.

Which is obviously incorrect because the century number is not there and also the zero for the year.

我碰到了一些2000年以前的日期,并且所有格式都已关闭.是否有任何简单的方法可以翻转日期,或者我是否必须为2000年之前的日期设置第二个数组?

I just ran in to some dates that were pre 2000 in year and all of the formatting is off. Is there any easy way to flip the date around or will I have to have a second array arrangement for dates that are pre 2000?

推荐答案

CYMD 该日期具有世纪,年,月,日的格式,cyymmdd,其中1928年至1999年的c为0,2000年至2071年的c为1.

CYMD The date has the century, year, month, day format, cyymmdd, where c is 0 for years 1928 through 1999 and is 1 for years 2000 through 2071.

为您量身定做: http://ideone.com/6MQmWk

<?php

function cymdToTime($d) {
$matches = null;
preg_match('/^(\\d*)?(\\d\\d)(\\d\\d)(\\d\\d)$/', $d, $matches);
return strtotime( (($matches[1] + 19) * 100 + $matches[2]) . '-' . $matches[3] . '-' . $matches[4]);
}

echo strftime('%B %d, %Y', cymdToTime(960614)); // June 14, 1996
echo strftime('%B %d, %Y', cymdToTime(1090225)); // February 25, 2009

这篇关于如何在PHP中将标准AS400 CYMD日期格式转换为MDY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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