views:

285

answers:

3

Hi,

In .net, there are the static properties DateTime.MinDate, and DateTime.MaxDate that conveniently return the minimum and maximum valid dates for a DateTime object.

I'm dabbling in web programming right now, using php + mysql + javascript. There doesn't seem to be the same convenient min/max date values in that programming environment? For example, the max value of a date object in mysql is 9999-12-31, but the php function strtotime() doesn't like that value. I would like a cross-language minimum date (to be used to mean 'not set yet' for example), and a cross-language maximum date (to be used to mean 'good forever'). That means there could be those min dates and max dates stored in a database, which php would retrieve (and it would have to differentiate between 'normal' dates and min/max date), and eventually they would trickle down to some javascript (which, again would have to differentiate between 'normal' dates and min/max date).

So, which date value do you use for min/max dates when working in php + mysql + javascript? And how do you store these constants -- it'd be nice to define them only in one place and have them be available in each of php + mysql + javascript...

Thanks

A: 

I'll just answer the PHP portion of the question. According to the PHP date() documentation:

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)

PHP uses 32 bit integer values to represent date/time — that means you can use the PHP_INT_MAX constant to derive the integer values associated with the min/max dates:

echo date('m/d/Y G:i:s', PHP_INT_MAX + 1); // minimum valid date
echo date('m/d/Y G:i:s', PHP_INT_MAX); // maximum valid date

OUTPUT:

12/13/1901 15:45:52

01/18/2038 22:14:07

Not sure why that's off by 2 seconds on the min date they quoted, but you get the general idea.

pjbeardsley
Looks like php's 32-bit date format is the limiting factor (compared to mysql and javascript), so it looks like this is the one to use. Kind of sucks, 2038 is not very far away.
Jimmy
A: 

For the JavaScript side, the range is a lot bigger:

The date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

So you could do this in your JavaScript:

var min_date = newDate(-100000000*86400000);
var max_date = newDate( 100000000*86400000);   
Ryley
A: 

For the 'not set yet' logical value, maybe a null value is better.

Iacopo
Yes, sometimes that's ok, but in my particular case, I can treat 'not set' as a date in the past. I'd like to pick a consistent date in the past, and some sort of mindate would work well. Again, this is specific to my situation, but in this case, a mindate would work better than a null date.
Jimmy