0

How do insert a Date before the year 1900 into MongoDB from PHP? To be more precise, I need to start from a date string, e.g. "03 Aug 1492" and insert it into a collection as an ISODate object.

Handling dates before 1900 in PHP is done using DateTime. I can easily create a DateTime object from my date string:

$date = DateTime::createFromFormat('d M Y', '21 Feb 1554');

But how do I get from there to a correct insert, e.g.:

$db->collection->insert([
    'name': 'Jon Doe',
    'date': $isoDate
])

In other words, how do I convert my $date into an ISODate object (not a string)?

David
  • 3,075
  • 3
  • 26
  • 36
  • $date->format('Y-m-d'); – John Conde Sep 21 '15 at 19:41
  • @JohnConde - $date->format('Y-m-d') will save the date as a string, not an object. – David Sep 21 '15 at 19:44
  • What would an example of that look like? I do not use MongoDB so I am unfamiliar with how this should look. – John Conde Sep 21 '15 at 19:46
  • The mongodb documentation contains the example 2012-12-19T06:01:17.171Z which is a fomat only used for ISO 8601 dates. ISO 8601 dates are ALWAYS in the Gregorian calendar. One of your examples, 3 Aug 1492, is the launch date of Columbus's voyage to the New World, is in the Julian calendar. If you actually intend to work dates before 1923, you need to be aware of when the Gregorian calendar was adopted in each country you are interested in. – Gerard Ashton Sep 21 '15 at 20:23
  • http://stackoverflow.com/questions/13448614/how-to-return-iso-date-format-in-php-for-mongodb?rq=1 – chiliNUT Oct 14 '15 at 04:29

0 Answers0