Oleg
Oleg

Reputation: 3014

php - Convert String To Date

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?

Upvotes: 0

Views: 1129

Answers (3)

Taha Paksu
Taha Paksu

Reputation: 15616

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

Upvotes: 3

nickb
nickb

Reputation: 59699

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

Upvotes: 4

deceze
deceze

Reputation: 522005

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.

Upvotes: 1

Related Questions