0

I'm trying to convert given string in format 1899-12-30 19:00:00.000 to date using this function:

$startDate = strtotime("1899-12-30 19:00:00.000");

but as a result I get a wrong date, e.g.

30.10.2008 00:00:00

what am I doing wrong?

2
  • What's $array? Should [i] be [$i]? Commented Jul 23, 2012 at 13:30
  • 2
    This is incidentally just outside the range php can deal with these ancient functions. Why 1899 ? Commented Jul 23, 2012 at 13:34

3 Answers 3

4

Use DateTime:

$date = DateTime::createFromFormat( 'Y-m-d H:i:s.u', '1899-12-30 19:00:00.000', new DateTimeZone( 'America/New_York'));
echo $date->getTimestamp() . ' ' . $date->format( 'Y-m-d H:i:s');

Note that this will be a negative value since it's before Jan 1st, 1970. The above code outputs:

-2209093200 1899-12-30 19:00:00 

Make sure you set the correct timezone in the constructor to DateTimeZone (or you can omit it to leave it as default).

Sign up to request clarification or add additional context in comments.

Comments

3

if you are using PHP 5 >= 5.3.0 you can use date_parse_from_formatDocs function like this:

$date = "1899-12-30 19:00:00.000";
$dateObj = date_parse_from_format("Y-m-d H:i:s.000",$date);

Source : php.net

1 Comment

I don't believe your format string is correct - It does not return the correct results in my test.
1

The date 1899-12-30 19:00:00.000 is represented by the UNIX timestamp -2209096800, which is just outside the range of a 32 bit signed integer. On a 64 bit PHP system it will work, on a 32 bit system you'll run into problems.

Use the DateTime class to represent such dates, it can handle it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.