shaikh
shaikh

Reputation: 1395

PHP date issue, can't convert date before 1970-01-01

The following code outputs 1970-01-01 which is wrong.

<?php
$dob='17 Jan 1900';
$datetime = strtotime($dob);
$dob = date("Y-m-d", $datetime);
echo $dob;
?>

However it works fine with $dob = '17 Jan 2000';

Upvotes: 2

Views: 5129

Answers (2)

grunk
grunk

Reputation: 14938

If your PHP version allow it consider Using DateTime instead of strtotime :

$date = DateTime::createFromFormat('d M Y','17 Jan 1900');
echo $date->format('Y-m-d');

For PHP version between >= 5.2 and <= 5.3 simply use the DateTime constructor :

$date = new DateTime('17 Jan 1900');
echo $date->format('Y-m-d');

Upvotes: 9

Shakti Singh
Shakti Singh

Reputation: 86406

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

However, You can use the PHP DateTIme class.

Upvotes: 5

Related Questions