[drupal-devel] date conversion
Hi, I was wondering if anyone knows of a decent method of converting dates from text to a time. I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35. Is there is improved strtotime() that will convert to anything to a time. Any help will be most appreciated. Gordon.
On Mon, 13 Jun 2005, Gordon Heydon wrote:
I was wondering if anyone knows of a decent method of converting dates from text to a time.
I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35.
Note: 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.) (from my version of the php manual entry for strtotime) print strtotime("60 years ago"); gives me -774821398. print date("d m y", strtotime("60 years ago")); gives me 13 06 45. Only on windows you will run into problems since it does not support negative timestamps.
From the date() docs: Note: 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). On windows this range is limited from 01-01-1970 to 19-01-2038.
To generate a timestamp from a string representation of the date, you may be able to use strtotime(). Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL's UNIX_TIMESTAMP function).
From the MySQL manual I infer that MySQL does not support negative timestamps.
Cheers, Gerhard
Hi, On Mon, 2005-06-13 at 06:59 +0200, Gerhard Killesreiter wrote:
On Mon, 13 Jun 2005, Gordon Heydon wrote:
I was wondering if anyone knows of a decent method of converting dates from text to a time.
I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35.
Note: 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.)
(from my version of the php manual entry for strtotime)
print strtotime("60 years ago");
gives me -774821398.
Well that gives me on both my ubuntu and debian linux systems both give me -1, It does say on the php site that it is windows and some linux distributions. So if I use strtotime('31 Dec 1969') I get a -1, If it was giving me the rangi that it would say it would me ok. Thanks. Gordon
On Mon, 13 Jun 2005, Gordon Heydon wrote:
Hi, On Mon, 2005-06-13 at 06:59 +0200, Gerhard Killesreiter wrote:
On Mon, 13 Jun 2005, Gordon Heydon wrote:
I was wondering if anyone knows of a decent method of converting dates from text to a time.
I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35.
Note: 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.)
(from my version of the php manual entry for strtotime)
print strtotime("60 years ago");
gives me -774821398.
Well that gives me on both my ubuntu and debian linux systems both give me -1, It does say on the php site that it is windows and some linux distributions.
He, that's annoying. I tested on Debian too, and it fails. My earlier test was on a rather ancient SuSE distribution.
So if I use strtotime('31 Dec 1969') I get a -1, If it was giving me the rangi that it would say it would me ok.
http://de3.php.net/strtotime In the comments there is a safestrtotime() function that claims to support negative timestamps. Haven't tried it, though. Cheers, Gerhard
Hi, I have managed to work around this by converting a javascript date parsing function that was inspired by strtotime(). http://simon.incutio.com/archive/2003/10/06/betterDateInput Basically it uses regular expressions to create a very clean implementation of a data parser. As the gmmkdate() was not broken on linux (but is on windows so I used win_gmmkdate) I reimplemented this in php, and work really good. it most likely will not scale very well as it users preg_test() and preg_grep() to do the matching but for just validating and converting dates from forms it works extremely well. Gordon. On Mon, 2005-06-13 at 11:38 +0200, Gerhard Killesreiter wrote:
On Mon, 13 Jun 2005, Gordon Heydon wrote:
Hi, On Mon, 2005-06-13 at 06:59 +0200, Gerhard Killesreiter wrote:
On Mon, 13 Jun 2005, Gordon Heydon wrote:
I was wondering if anyone knows of a decent method of converting dates from text to a time.
I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35.
Note: 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.)
(from my version of the php manual entry for strtotime)
print strtotime("60 years ago");
gives me -774821398.
Well that gives me on both my ubuntu and debian linux systems both give me -1, It does say on the php site that it is windows and some linux distributions.
He, that's annoying. I tested on Debian too, and it fails. My earlier test was on a rather ancient SuSE distribution.
So if I use strtotime('31 Dec 1969') I get a -1, If it was giving me the rangi that it would say it would me ok.
In the comments there is a safestrtotime() function that claims to support negative timestamps. Haven't tried it, though.
Cheers, Gerhard
!DSPAM:42ad557729171880090616!
I was wondering if anyone knows of a decent method of converting dates from text to a time.
I did look at strtotime() but it will not convert anything before Jan 1, 1970 which is a problem since it is going to be used for a date of birth field, and some people will be over 35.
Is there is improved strtotime() that will convert to anything to a time.
As Killes has pointed out, negative timestamps are a problem on Windows (and so I've heard, on some of the rarer Unix variants too). This is why profile.module has its own routines for handling date fields (as birthdays are a common use case). The day, month and year are stored separately (well, as a serialized triple). The downside is that non-existant dates like "31 February" can be entered. Without more information there's not much to say... but converting a string representation of a birthday to a day/month/year pair is quite simple as long as the format is not ambiguous (e.g. d/m/y vs m/d/y). Perhaps you could try something in that direction. Steven
participants (3)
-
Gerhard Killesreiter -
Gordon Heydon -
Steven Wittens