0

i am converting 'd-m-Y' format to 'Y-m-d'

Currently, I am using this code

$dp_date = '13-12-1901';
$formated_date = date("Y-m-d",strtotime($dp_date));

but not working with dates before 14-12-1901 and works fine after this date '13-12-1901' as input.

whats the problem here? and how can i do this formatting for the dates before 1900, or 14-12-1901 to be specific?

Andy G
  • 19,232
  • 5
  • 47
  • 69
Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43
  • Use DateTime objects, which support ranges well outside those of a unix timestamp, which will allow you to work with dateranges that a Paleobiologist or cosmologist would be happy with – Mark Baker Sep 02 '13 at 19:38
  • @MarkBaker Just mentioned your answer to a similar question... – Ofir Baruch Sep 02 '13 at 19:40

2 Answers2

2

It's a known limitation of Unix timestamp.

As explained in php.net:

strtotime()

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

BTW , if all you want to do is to replace the position of d and Y you can do it with string manipulation.

UPDATE

strtotime() has a range limit between Fri, 13 Dec 1901 20:45:54 GMT and Tue, 19 Jan 2038 03:14:07 GMT; although prior to PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some operating systems (Windows).

@MarkBaker https://stackoverflow.com/a/2872094/998096

He suggested "using PHP's datetime objects which can work with a much wider range of dates".

Community
  • 1
  • 1
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
1

Simply use PHP's DateTime class (PHP 5.3+). It's a lot better and is known to work with a wider range of dates.

An example:

$dp_date = '13-12-1901';
$date = new DateTime($dp_date);
echo $date->format('Y-m-d');

should output:

1901-12-13

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150