Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Forcing a user to enter a date as a specifically formatted string is so last millennium. We're using PHP, so there's absolutely no reason to enter a date the hard way... There's a beautiful function in PHP called strtotime() that magically converts almost any human representation of a date into a timestamp. How much cooler is this?
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
I coded up support for strtotime() style dates, and I'm testing it on a production box as we speak. If it holds up, I'll submit a patch.
Comment | File | Size | Author |
---|---|---|---|
#6 | scheduler-strtotime-6.x-1.3.patch | 3.5 KB | bobthecow |
#4 | scheduler-strtotime.patch | 4.03 KB | bobthecow |
Comments
Comment #1
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedI believe people have thought about this before.
I cant test it right now, does it magically convert
"jetzt"
"nächsten Dienstag"
"+1 Woche 2Tage 4 Stunden 2 Sekunden"
?
Comment #2
bobthecow CreditAttribution: bobthecow commentedYou know, you're absolutely right. It's not completely magic. I noticed that when I was writing it, and it bugged me. I wish PHP automatically handled more than just English in the strtotime function. Unfortunately it doesn't.
But it does handle:
2005/08/15 3:52:01 pm
2005/08/15 15:52:01
2005-08-15T15:52:01+00:00
15-Aug-05 15:52:01
Monday, 15-Aug-05 15:52:01
15 Aug 05 15:52:01 +0000
Mon, 15 Aug 05 15:52:01 +0000
Monday, 15 Aug 05 15:52:01 +0000
Monday, 15-Aug-05 15:52:01 UTC
15 Aug 05 15:52:01 +0000
As well as almost about any other standardized, or not-so-standard, date format I could think of. You're right though. It's not fair that non-english speaking users can't use month/day/hour offset, or natural language date strings. But I'm sure everyone would still benefit from the fairly format agnostic interpretation of date/time strings as seen above. It's still fairly magic, especially compared to a "format it exactly like this" text field.
I imagine a more "fair" date string parser could be implemented by wrapping calls to strtotime() in another function that thunked the user/site localized words for 'day', 'month', days of the week, etc, into US English before running strtotime().
Ultimately, it's about 10 lines of code to add the option of a more intuitive date input field. In my implementation, you enter 'strtotime' instead of a date format on the module config page to trigger strtotime() date handling. It's not the default format, it's not forced on you. It's just there if you want it.
And I sure do.
Comment #3
mglover CreditAttribution: mglover commentedYou're not the only one who wants this, bobthecow. Would you be willing to post a patch?
Comment #4
bobthecow CreditAttribution: bobthecow commentedHere's a strtotime() patch against 6.x-1.2. It's working well on my site. Let me know if you have any problems..
Comment #5
bobthecow CreditAttribution: bobthecow commentedTo use this: enter 'strtotime' in the date format box.
Comment #6
bobthecow CreditAttribution: bobthecow commentedHere's a version bump on the patch, for anyone who wants it. This patch is the same as above, rolled against Scheduler 6.x-1.3
Comment #7
zilla CreditAttribution: zilla commentedwill this make it into the core of the module at some point? wondering if the devs have any opinion on logical and simple date choices..
what about date popup?
Comment #8
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedThis collides with #250210: Integrate the new jquery popup calendar from Date module version 2 which is not ready yet. When it is ready I will try to get both things working. Please be patient.
BTW: Testing Bobs patch and sharing the result would speed things up a bit... ;-)
Comment #9
zilla CreditAttribution: zilla commentedthanks eric - looking forward to seeing it! this is an excellent module, and in fact it's a feature that i always thought would be built into core considering that drupal is CMS and content is not always 'unpublished' but rather moves through draft and save and schedule dates in a normal content production environment (magazine, newspaper, portal, etc)
Comment #10
jonathan1055 CreditAttribution: jonathan1055 commentedYou asked for some test results, here is what I have found so far:
I hope these comments are helpful. Scheduler is a great module (thanks AjK, Eric and the rest) but this patch is certainly not ready to be committed, as it reduces the excellent interface to a slightly clumsy experience. I am sure the patch could be improved, and it would be worth the effort I think.
Comment #11
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedThank you very much, for testing.Although I would probably not be using this feature I would like to see it implemented.
Unfortunately #250210: Integrate the new jquery popup calendar from Date module version 2 didn't have any progress for the last couple of weeks (months?). So if anyone is willing to help making the date/time input easier, please have a look at the popup-feature.
Comment #12
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedThere are time zone issues with the latest patch. I will dig into this this week...
Comment #13
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedOh, by the way, this type of input does not work with the date popup active. So the admin would need to deactivate that module. But it might be needed for other modules. Should we make the use of data popup configurable or deactivate it if the date format is 'strtotime'?
Comment #14
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedThe time zone issues are easy to resolve.
There are serious issues with using the display date format ("date_format_long") because this may be parsed again later (when editing the node) and that will fail, because it might not be a format recognized by strtotime(). How about this: We do not touch the input format and introduce a checkbox "Allow funny dates" ;-). We could then let _scheduler_strptime() try to parse it and if it fails we pass it to strtotime. When the node is edited the times are rendered using the user provided input format (as it is done now) and if the node is saved again the time can be parsed successfully.
Comment please.
Comment #15
Eric-Alexander Schaefer CreditAttribution: Eric-Alexander Schaefer commentedI can as well close this issue if nobody cares about it.
Comment #16
jonathan1055 CreditAttribution: jonathan1055 commentedHi Eric,
Sorry, I've not been contributing to Scheduler these last few months, got involved with other modules.
Referring to my testing in #10 unless these are fixed then it should not be implemented, so it would need some work. But now that you have spent so much effort to good use on the jquery pop-up, if you want to close this thread, that is fine with me.
Jonathan