开发者

Error creating Java Date in php

Im trying to create a php function that converts a string date to a java date to pass to java. I need to use java.sql.Date not java.util.Date for my application. But this simple function is giving an odd result:

function makeStrJavaDate($date){
    $date = date_parse($date);
    $date = new Java("java.sql.Date",$date['year'], $date['month']-1, $date['day']);
    return $date;
}

The date java returned has the wrong year: php (For 2011-07-01), java(Jul 1, 3911).

Any pointers as to whats wrong?

FYI, php is parsing it correctly:

Array
(开发者_StackOverflow
    [year] => 2011
    [month] => 7
    [day] => 1
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 
)


Evidently, it's considering the year to start at 1900. So subtract 1900 when you pass the year.


It looks like a bizarro variant of Y2K (you're off by 1900 years). You can fix this by subtracting 1900 years from $date['year'], but I'm wondering if you wouldn't be better off using strtotime and just passing in a timestamp, especially since Date(int year, int month, int day) is deprecated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜