0

I am trying to separate time from the given string date & time. I simply tried with following example.

$time = '2014-01-20 10:45:45';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));

I am getting correct date and time.

10:45:45
20 January 2014

But when I tried with given string, no correct result.

$time = '1899-12-30 19:30:00';
echo '<br>'.$finalTime = date("H:i:s",strtotime($time));
echo '<br>'.$date = date("d F Y",strtotime($time));

PHP is always returning me following result.

00:00:00
01 January 1970

I am not sure whether is there any limitation on date function that is not returning 1899. Is that so?

Kamal Joshi
  • 1,298
  • 3
  • 25
  • 45

2 Answers2

3

Your date is before the unix epoch. DateTime() allows you to work around that.

$dt = new DateTime("1899-12-30 19:30:00");
echo $dt->format("d F Y");
echo $dt->format("h:i:s");
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

Use DateTime and DateTime::format():

$time = '1899-12-30 19:30:00';
$dt = new DateTime($time);
echo $dt->format('d F Y H:i:s');

Working example: http://3v4l.org/fM22Z

The strtotime() method is limited by the Unix epoch, which is Jan 1, 1970.

Update: As Mark comments below, your code would work all the way back to 1901 (as a negative timestamp), see here: http://3v4l.org/CSJte

jszobody
  • 28,495
  • 6
  • 61
  • 72