Вопрос

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

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.

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 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.

Just made it for you: 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

Другие советы

A simple UDF can be used to convert CYMD to SQL DATE format on the host:

CREATE FUNCTION QGPL.CYMDTODATE(IN DEC(7)) RETURNS DATE
LANGUAGE SQL
DETERMINISTIC
RETURNS NULL ON NULL INPUT
BEGIN
    RETURN(DATE(
        SUBSTR(DIGITS(19000000+IN),2,4) || '-' ||
        SUBSTR(DIGITS(IN),4,2) || '-' ||
        SUBSTR(DIGITS(IN),6,2)));
END

It can be used like this:

SELECT QGPL.CYMDTODATE(COMPDT) COMPDT 
FROM MYLIB.MYTABLE
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top