Error creating Java Date in php

555

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
(
    [year] => 2011
    [month] => 7
    [day] => 1
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] => 
)
334

Answer

Solution:

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

929

Answer

Solution:

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 usingstrtotime and just passing in a timestamp, especially sinceDate(int year, int month, int day) is deprecated.

People are also looking for solutions to the problem: php - PDO pass by reference notice?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.