Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php / mysql / javascript mindate and maxdate

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

like image 829
Jimmy Avatar asked Jun 09 '10 20:06

Jimmy


2 Answers

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 = new Date(-100000000*86400000);
var max_date = new Date( 100000000*86400000);   
like image 126
Ryley Avatar answered Nov 11 '22 12:11

Ryley


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.

like image 2
pjbeardsley Avatar answered Nov 11 '22 12:11

pjbeardsley