0

I insert date fields into MongoDB collection using PHP-MongoDate.

For 05/29/2014, i tried like below

new MongoDate(1401321600) and it creates ISODate("2014-05-29T00:00:00Z") in Mongocollection which is correct

But for 05/28/1857, i tried like below

new MongoDate(-3553200000) and it creates ISODate("1993-07-04T06:28:16Z") in Mongocollection which is wrong

But for 05/29/2045, i tried like below

new MongoDate(2379628800) and it creates ISODate("1909-04-22T17:31:44Z") in Mongocollection which is wrong

PHP MongoDate is not working after / before certain year. I'm clueless about this issue. Any one know the solution for it?

Charles
  • 50,943
  • 13
  • 104
  • 142
user10
  • 5,186
  • 8
  • 43
  • 64
  • 2
    Seems to be a 32-bit unix timestamp issue, which can't handle dates before 1970 january or after 2038 may (not sure about the may). – Maerlyn Jan 23 '14 at 18:46
  • @Maerlyn Do you have any idea to fix this crazy issue? – user10 Jan 24 '14 at 03:02
  • What version of the MongoDB PHP driver are you using? According to [PHP-423](https://jira.mongodb.org/browse/PHP-423), MongoDate has support for 64-bit timestamps on 32-bit systems as of the 1.4.3 driver release. – Stennie Jan 24 '14 at 13:29

2 Answers2

1

The problem is related to the range of values that can be represented in a 32-bit integer versus a 64-bit integer. If you have a 64-bit PHP build, the MongoDate class should work as expected.

For 32-bit PHP builds, the range of date values that can be represented is normally between 1-Jan-1970 and 19-Jan-2038. This is the same as the standard Unixtime format.

The MongoDB driver includes some tricks to try to work around the limitations of working with 64-bit numbers on 32-bit systems, including a MongoInt64 class that wrangles the string values of a 64-bit number.

If you upgrade to the MongoDB 1.4.3 PHP driver release or newer, the MongoDate class should have support for 64-bit timestamps on 32-bit systems (see PHP-423).

If possible, a more reliable option would be to upgrade to a 64-bit version of PHP.

Stennie
  • 63,885
  • 14
  • 149
  • 175
  • Thanks for your answer. But this is not fair. Are everyone doing this in same way? Is it a bug in Mongo PHPDriver? Didn't they address this problem? What is the use of MongoDate then? – user10 Jan 24 '14 at 10:53
  • @user10: I did some further digging and it looks like there is a much better answer. Unfortunately I don't have a 32-bit system handy to test with, so please comment if upgrading to the PHP 1.4.3 driver (or newer) does or doesn't work for you. – Stennie Jan 24 '14 at 13:38
  • i was using 1.3.2.0 before. But after your comment i changed to 1.4.5.0. No luck still. I'm on windows 7 - 64 bit operating system and i'm using 32 bit PHP version. FYI, this is my local development setup. Our server is running with linux. If i fix this problem in my local development, i feel i can fix in server too. – user10 Jan 24 '14 at 19:45
  • Is there any reason you need to run 32-bit PHP on 64-bit Windows? Upgrading to 64-bit PHP should solve the issue. Also, what version of PHP are you using (`php -v`)? – Stennie Jan 24 '14 at 19:48
  • I installed 64 bit PHP. But still there isn't any luck. please see this answer http://stackoverflow.com/questions/864058/how-to-have-64-bit-integer-on-php#3233881. – user10 Jan 25 '14 at 06:05
  • I installed ***Wampserver (64 bits & PHP 5.3) 2.2E*** from this website http://www.wampserver.com/en/. Still no luck. – user10 Jan 25 '14 at 07:50
0

I've come up against this problem too, and the new driver (1.4.3) doesn't work. By the sound of the comments in that fix, the problem was only partially solved, because it isn't possible to create a MongoDate after 2038 or before 1902 using only PHP.

It's annoying because 32-bit PHP can handle this range of dates, and so can MongoDB. The sticking point is the driver, which can't manage.

It will only really be solved when issue 370 gets fixed (https://jira.mongodb.org/browse/PHP-370).

In the meantime the best solution seems to be to store dates (on the database) as strings, and then convert the strings to PHP DateTimes as necessary.

blah
  • 1