Skip to content

Commit e3b8b8d

Browse files
committed
Reinstate appropriate changes by @Ivanca on 2014/01/03; however, add "date_parse_state" property detection (on the php_js "global") in strtotime so date_parse can utilize this to get strtotime to return a decimal without changing the normal, integer-only behavior of strtotime
1 parent d2b5611 commit e3b8b8d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

functions/datetime/date_parse.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function date_parse(date) {
1313
var errorsOffset = this.php_js.errors ? this.php_js.errors.length : null;
1414

1515
try {
16+
this.php_js.date_parse_state = true; // Allow strtotime to return a decimal (which it normally does not)
1617
var ts = this.strtotime(date);
18+
this.php_js.date_parse_state = false;
1719
} finally {
1820
if (!ts) {
1921
return false;

functions/datetime/strtotime.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ function strtotime(text, now) {
2121
// * returns 3: 1127041200
2222
// * example 4: strtotime('2009-05-04 08:30:00');
2323
// * returns 4: 1241418600
24-
var parsed, match, year, date, days, ranges, len, times, regex, i;
24+
var parsed, match, year, date, days, ranges, len, times, regex, i,
25+
_ret = function (rt) {
26+
return this.php_js && this.php_js.date_parse_state ? rt : rt | 0; // phpjs custom check to allow fractional results as required by date_parse
27+
};
2528

2629
if (!text) {
2730
return null;
@@ -34,14 +37,14 @@ function strtotime(text, now) {
3437
.toLowerCase();
3538

3639
if (text === 'now') {
37-
return now === null || isNaN(now) ? new Date().getTime() / 1000 : now;
40+
return _ret(now === null || isNaN(now) ? new Date().getTime() / 1000 : now);
3841
}
3942

4043
match = text.match(/^(\d{2,4})-(\d{2})-(\d{2})(?:\s(\d{1,2}):(\d{2})(?::(\d{2}))?)?(?:\.(\d+)?)?$/);
4144
if (match) {
4245
year = match[1] >= 0 && match[1] <= 69 ? + match[1] + 2000 : match[1];
43-
return new Date(year, parseInt(match[2], 10) - 1, match[3],
44-
match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1000;
46+
return _ret(new Date(year, parseInt(match[2], 10) - 1, match[3],
47+
match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1000);
4548
}
4649

4750
date = now ? new Date(now * 1000) : new Date();
@@ -130,5 +133,5 @@ function strtotime(text, now) {
130133
//if (!match.every(process))
131134
// return false;
132135

133-
return (date.getTime() / 1000);
136+
return _ret(date.getTime() / 1000);
134137
}

0 commit comments

Comments
 (0)