PHP's strtotime in JavaScript

Here’s what our current JavaScript equivalent to PHP's strtotime looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
const reSpace = '[ \\t]+'
const reSpaceOpt = '[ \\t]*'
const reMeridian = '(?:([ap])\\.?m\\.?([\\t ]|$))'
const reHour24 = '(2[0-4]|[01]?[0-9])'
const reHour24lz = '([01][0-9]|2[0-4])'
const reHour12 = '(0?[1-9]|1[0-2])'
const reMinute = '([0-5]?[0-9])'
const reMinutelz = '([0-5][0-9])'
const reSecond = '(60|[0-5]?[0-9])'
const reSecondlz = '(60|[0-5][0-9])'
const reFrac = '(?:\\.([0-9]+))'

const reDayfull = 'sunday|monday|tuesday|wednesday|thursday|friday|saturday'
const reDayabbr = 'sun|mon|tue|wed|thu|fri|sat'
const reDaytext = reDayfull + '|' + reDayabbr + '|weekdays?'

const reReltextnumber = 'first|second|third|fourth|fifth|sixth|seventh|eighth?|ninth|tenth|eleventh|twelfth'
const reReltexttext = 'next|last|previous|this'
const reReltextunit = '(?:second|sec|minute|min|hour|day|fortnight|forthnight|month|year)s?|weeks|' + reDaytext

const reYear = '([0-9]{1,4})'
const reYear2 = '([0-9]{2})'
const reYear4 = '([0-9]{4})'
const reYear4withSign = '([+-]?[0-9]{4})'
const reMonth = '(1[0-2]|0?[0-9])'
const reMonthlz = '(0[0-9]|1[0-2])'
const reDay = '(?:(3[01]|[0-2]?[0-9])(?:st|nd|rd|th)?)'
const reDaylz = '(0[0-9]|[1-2][0-9]|3[01])'

const reMonthFull = 'january|february|march|april|may|june|july|august|september|october|november|december'
const reMonthAbbr = 'jan|feb|mar|apr|may|jun|jul|aug|sept?|oct|nov|dec'
const reMonthroman = 'i[vx]|vi{0,3}|xi{0,2}|i{1,3}'
const reMonthText = '(' + reMonthFull + '|' + reMonthAbbr + '|' + reMonthroman + ')'

const reTzCorrection = '((?:GMT)?([+-])' + reHour24 + ':?' + reMinute + '?)'
const reDayOfYear = '(00[1-9]|0[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-6])'
const reWeekOfYear = '(0[1-9]|[1-4][0-9]|5[0-3])'

function processMeridian (hour, meridian) {
meridian = meridian && meridian.toLowerCase()

switch (meridian) {
case 'a':
hour += hour === 12 ? -12 : 0
break
case 'p':
hour += hour !== 12 ? 12 : 0
break
}

return hour
}

function processYear (yearStr) {
let year = +yearStr

if (yearStr.length < 4 && year < 100) {
year += year < 70 ? 2000 : 1900
}

return year
}

function lookupMonth (monthStr) {
return {
jan: 0,
january: 0,
i: 0,
feb: 1,
february: 1,
ii: 1,
mar: 2,
march: 2,
iii: 2,
apr: 3,
april: 3,
iv: 3,
may: 4,
v: 4,
jun: 5,
june: 5,
vi: 5,
jul: 6,
july: 6,
vii: 6,
aug: 7,
august: 7,
viii: 7,
sep: 8,
sept: 8,
september: 8,
ix: 8,
oct: 9,
october: 9,
x: 9,
nov: 10,
november: 10,
xi: 10,
dec: 11,
december: 11,
xii: 11
}[monthStr.toLowerCase()]
}

function lookupWeekday (dayStr, desiredSundayNumber = 0) {
const dayNumbers = {
mon: 1,
monday: 1,
tue: 2,
tuesday: 2,
wed: 3,
wednesday: 3,
thu: 4,
thursday: 4,
fri: 5,
friday: 5,
sat: 6,
saturday: 6,
sun: 0,
sunday: 0
}

return dayNumbers[dayStr.toLowerCase()] || desiredSundayNumber
}

function lookupRelative (relText) {
const relativeNumbers = {
last: -1,
previous: -1,
this: 0,
first: 1,
next: 1,
second: 2,
third: 3,
fourth: 4,
fifth: 5,
sixth: 6,
seventh: 7,
eight: 8,
eighth: 8,
ninth: 9,
tenth: 10,
eleventh: 11,
twelfth: 12
}

const relativeBehavior = {
this: 1
}

const relTextLower = relText.toLowerCase()

return {
amount: relativeNumbers[relTextLower],
behavior: relativeBehavior[relTextLower] || 0
}
}

function processTzCorrection (tzOffset, oldValue) {
const reTzCorrectionLoose = /(?:GMT)?([+-])(\d+)(:?)(\d{0,2})/i
tzOffset = tzOffset && tzOffset.match(reTzCorrectionLoose)

if (!tzOffset) {
return oldValue
}

let sign = tzOffset[1] === '-' ? 1 : -1
let hours = +tzOffset[2]
let minutes = +tzOffset[4]

if (!tzOffset[4] && !tzOffset[3]) {
minutes = Math.floor(hours % 100)
hours = Math.floor(hours / 100)
}

return sign * (hours * 60 + minutes)
}

const formats = {
yesterday: {
regex: /^yesterday/i,
name: 'yesterday',
callback () {
this.rd -= 1
return this.resetTime()
}
},

now: {
regex: /^now/i,
name: 'now'
// do nothing
},

noon: {
regex: /^noon/i,
name: 'noon',
callback () {
return this.resetTime() && this.time(12, 0, 0, 0)
}
},

midnightOrToday: {
regex: /^(midnight|today)/i,
name: 'midnight | today',
callback () {
return this.resetTime()
}
},

tomorrow: {
regex: /^tomorrow/i,
name: 'tomorrow',
callback () {
this.rd += 1
return this.resetTime()
}
},

timestamp: {
regex: /^@(-?\d+)/i,
name: 'timestamp',
callback (match, timestamp) {
this.rs += +timestamp
this.y = 1970
this.m = 0
this.d = 1
this.dates = 0

return this.resetTime() && this.zone(0)
}
},

firstOrLastDay: {
regex: /^(first|last) day of/i,
name: 'firstdayof | lastdayof',
callback (match, day) {
if (day.toLowerCase() === 'first') {
this.firstOrLastDayOfMonth = 1
} else {
this.firstOrLastDayOfMonth = -1
}
}
},

backOrFrontOf: {
regex: RegExp('^(back|front) of ' + reHour24 + reSpaceOpt + reMeridian + '?', 'i'),
name: 'backof | frontof',
callback (match, side, hours, meridian) {
let back = side.toLowerCase() === 'back'
let hour = +hours
let minute = 15

if (!back) {
hour -= 1
minute = 45
}

hour = processMeridian(hour, meridian)

return this.resetTime() && this.time(hour, minute, 0, 0)
}
},

weekdayOf: {
regex: RegExp('^(' + reReltextnumber + '|' + reReltexttext + ')' + reSpace + '(' + reDayfull + '|' + reDayabbr + ')' + reSpace + 'of', 'i'),
name: 'weekdayof'
// todo
},

mssqltime: {
regex: RegExp('^' + reHour12 + ':' + reMinutelz + ':' + reSecondlz + '[:.]([0-9]+)' + reMeridian, 'i'),
name: 'mssqltime',
callback (match, hour, minute, second, frac, meridian) {
return this.time(processMeridian(+hour, meridian), +minute, +second, +frac.substr(0, 3))
}
},

timeLong12: {
regex: RegExp('^' + reHour12 + '[:.]' + reMinute + '[:.]' + reSecondlz + reSpaceOpt + reMeridian, 'i'),
name: 'timelong12',
callback (match, hour, minute, second, meridian) {
return this.time(processMeridian(+hour, meridian), +minute, +second, 0)
}
},

timeShort12: {
regex: RegExp('^' + reHour12 + '[:.]' + reMinutelz + reSpaceOpt + reMeridian, 'i'),
name: 'timeshort12',
callback (match, hour, minute, meridian) {
return this.time(processMeridian(+hour, meridian), +minute, 0, 0)
}
},

timeTiny12: {
regex: RegExp('^' + reHour12 + reSpaceOpt + reMeridian, 'i'),
name: 'timetiny12',
callback (match, hour, meridian) {
return this.time(processMeridian(+hour, meridian), 0, 0, 0)
}
},

soap: {
regex: RegExp('^' + reYear4 + '-' + reMonthlz + '-' + reDaylz + 'T' + reHour24lz + ':' + reMinutelz + ':' + reSecondlz + reFrac + reTzCorrection + '?', 'i'),
name: 'soap',
callback (match, year, month, day, hour, minute, second, frac, tzCorrection) {
return this.ymd(+year, month - 1, +day) &&
this.time(+hour, +minute, +second, +frac.substr(0, 3)) &&
this.zone(processTzCorrection(tzCorrection))
}
},

wddx: {
regex: RegExp('^' + reYear4 + '-' + reMonth + '-' + reDay + 'T' + reHour24 + ':' + reMinute + ':' + reSecond),
name: 'wddx',
callback (match, year, month, day, hour, minute, second) {
return this.ymd(+year, month - 1, +day) && this.time(+hour, +minute, +second, 0)
}
},

exif: {
regex: RegExp('^' + reYear4 + ':' + reMonthlz + ':' + reDaylz + ' ' + reHour24lz + ':' + reMinutelz + ':' + reSecondlz, 'i'),
name: 'exif',
callback (match, year, month, day, hour, minute, second) {
return this.ymd(+year, month - 1, +day) && this.time(+hour, +minute, +second, 0)
}
},

xmlRpc: {
regex: RegExp('^' + reYear4 + reMonthlz + reDaylz + 'T' + reHour24 + ':' + reMinutelz + ':' + reSecondlz),
name: 'xmlrpc',
callback (match, year, month, day, hour, minute, second) {
return this.ymd(+year, month - 1, +day) && this.time(+hour, +minute, +second, 0)
}
},

xmlRpcNoColon: {
regex: RegExp('^' + reYear4 + reMonthlz + reDaylz + '[Tt]' + reHour24 + reMinutelz + reSecondlz),
name: 'xmlrpcnocolon',
callback (match, year, month, day, hour, minute, second) {
return this.ymd(+year, month - 1, +day) && this.time(+hour, +minute, +second, 0)
}
},

clf: {
regex: RegExp('^' + reDay + '/(' + reMonthAbbr + ')/' + reYear4 + ':' + reHour24lz + ':' + reMinutelz + ':' + reSecondlz + reSpace + reTzCorrection, 'i'),
name: 'clf',
callback (match, day, month, year, hour, minute, second, tzCorrection) {
return this.ymd(+year, lookupMonth(month), +day) &&
this.time(+hour, +minute, +second, 0) &&
this.zone(processTzCorrection(tzCorrection))
}
},

iso8601long: {
regex: RegExp('^t?' + reHour24 + '[:.]' + reMinute + '[:.]' + reSecond + reFrac, 'i'),
name: 'iso8601long',
callback (match, hour, minute, second, frac) {
return this.time(+hour, +minute, +second, +frac.substr(0, 3))
}
},

dateTextual: {
regex: RegExp('^' + reMonthText + '[ .\\t-]*' + reDay + '[,.stndrh\\t ]+' + reYear, 'i'),
name: 'datetextual',
callback (match, month, day, year) {
return this.ymd(processYear(year), lookupMonth(month), +day)
}
},

pointedDate4: {
regex: RegExp('^' + reDay + '[.\\t-]' + reMonth + '[.-]' + reYear4),
name: 'pointeddate4',
callback (match, day, month, year) {
return this.ymd(+year, month - 1, +day)
}
},

pointedDate2: {
regex: RegExp('^' + reDay + '[.\\t]' + reMonth + '\\.' + reYear2),
name: 'pointeddate2',
callback (match, day, month, year) {
return this.ymd(processYear(year), month - 1, +day)
}
},

timeLong24: {
regex: RegExp('^t?' + reHour24 + '[:.]' + reMinute + '[:.]' + reSecond),
name: 'timelong24',
callback (match, hour, minute, second) {
return this.time(+hour, +minute, +second, 0)
}
},

dateNoColon: {
regex: RegExp('^' + reYear4 + reMonthlz + reDaylz),
name: 'datenocolon',
callback (match, year, month, day) {
return this.ymd(+year, month - 1, +day)
}
},

pgydotd: {
regex: RegExp('^' + reYear4 + '\\.?' + reDayOfYear),
name: 'pgydotd',
callback (match, year, day) {
return this.ymd(+year, 0, +day)
}
},

timeShort24: {
regex: RegExp('^t?' + reHour24 + '[:.]' + reMinute, 'i'),
name: 'timeshort24',
callback (match, hour, minute) {
return this.time(+hour, +minute, 0, 0)
}
},

iso8601noColon: {
regex: RegExp('^t?' + reHour24lz + reMinutelz + reSecondlz, 'i'),
name: 'iso8601nocolon',
callback (match, hour, minute, second) {
return this.time(+hour, +minute, +second, 0)
}
},

iso8601dateSlash: {
// eventhough the trailing slash is optional in PHP
// here it's mandatory and inputs without the slash
// are handled by dateslash
regex: RegExp('^' + reYear4 + '/' + reMonthlz + '/' + reDaylz + '/'),
name: 'iso8601dateslash',
callback (match, year, month, day) {
return this.ymd(+year, month - 1, +day)
}
},

dateSlash: {
regex: RegExp('^' + reYear4 + '/' + reMonth + '/' + reDay),
name: 'dateslash',
callback (match, year, month, day) {
return this.ymd(+year, month - 1, +day)
}
},

american: {
regex: RegExp('^' + reMonth + '/' + reDay + '/' + reYear),
name: 'american',
callback (match, month, day, year) {
return this.ymd(processYear(year), month - 1, +day)
}
},

americanShort: {
regex: RegExp('^' + reMonth + '/' + reDay),
name: 'americanshort',
callback (match, month, day) {
return this.ymd(this.y, month - 1, +day)
}
},

gnuDateShortOrIso8601date2: {
// iso8601date2 is complete subset of gnudateshort
regex: RegExp('^' + reYear + '-' + reMonth + '-' + reDay),
name: 'gnudateshort | iso8601date2',
callback (match, year, month, day) {
return this.ymd(processYear(year), month - 1, +day)
}
},

iso8601date4: {
regex: RegExp('^' + reYear4withSign + '-' + reMonthlz + '-' + reDaylz),
name: 'iso8601date4',
callback (match, year, month, day) {
return this.ymd(+year, month - 1, +day)
}
},

gnuNoColon: {
regex: RegExp('^t' + reHour24lz + reMinutelz, 'i'),
name: 'gnunocolon',
callback (match, hour, minute) {
return this.time(+hour, +minute, 0, this.f)
}
},

gnuDateShorter: {
regex: RegExp('^' + reYear4 + '-' + reMonth),
name: 'gnudateshorter',
callback (match, year, month) {
return this.ymd(+year, month - 1, 1)
}
},

pgTextReverse: {
// note: allowed years are from 32-9999
// years below 32 should be treated as days in datefull
regex: RegExp('^' + '(\\d{3,4}|[4-9]\\d|3[2-9])-(' + reMonthAbbr + ')-' + reDaylz, 'i'),
name: 'pgtextreverse',
callback (match, year, month, day) {
return this.ymd(processYear(year), lookupMonth(month), +day)
}
},

dateFull: {
regex: RegExp('^' + reDay + '[ \\t.-]*' + reMonthText + '[ \\t.-]*' + reYear, 'i'),
name: 'datefull',
callback (match, day, month, year) {
return this.ymd(processYear(year), lookupMonth(month), +day)
}
},

dateNoDay: {
regex: RegExp('^' + reMonthText + '[ .\\t-]*' + reYear4, 'i'),
name: 'datenoday',
callback (match, month, year) {
return this.ymd(+year, lookupMonth(month), 1)
}
},

dateNoDayRev: {
regex: RegExp('^' + reYear4 + '[ .\\t-]*' + reMonthText, 'i'),
name: 'datenodayrev',
callback (match, year, month) {
return this.ymd(+year, lookupMonth(month), 1)
}
},

pgTextShort: {
regex: RegExp('^(' + reMonthAbbr + ')-' + reDaylz + '-' + reYear, 'i'),
name: 'pgtextshort',
callback (match, month, day, year) {
return this.ymd(processYear(year), lookupMonth(month), +day)
}
},

dateNoYear: {
regex: RegExp('^' + reMonthText + '[ .\\t-]*' + reDay + '[,.stndrh\\t ]*', 'i'),
name: 'datenoyear',
callback (match, month, day) {
return this.ymd(this.y, lookupMonth(month), +day)
}
},

dateNoYearRev: {
regex: RegExp('^' + reDay + '[ .\\t-]*' + reMonthText, 'i'),
name: 'datenoyearrev',
callback (match, day, month) {
return this.ymd(this.y, lookupMonth(month), +day)
}
},

isoWeekDay: {
regex: RegExp('^' + reYear4 + '-?W' + reWeekOfYear + '(?:-?([0-7]))?'),
name: 'isoweekday | isoweek',
callback (match, year, week, day) {
day = day ? +day : 1

if (!this.ymd(+year, 0, 1)) {
return false
}

// get day of week for Jan 1st
let dayOfWeek = new Date(this.y, this.m, this.d).getDay()

// and use the day to figure out the offset for day 1 of week 1
dayOfWeek = 0 - (dayOfWeek > 4 ? dayOfWeek - 7 : dayOfWeek)

this.rd += dayOfWeek + ((week - 1) * 7) + day
}
},

relativeText: {
regex: RegExp('^(' + reReltextnumber + '|' + reReltexttext + ')' + reSpace + '(' + reReltextunit + ')', 'i'),
name: 'relativetext',
callback (match, relValue, relUnit) {
// todo: implement handling of 'this time-unit'
// eslint-disable-next-line no-unused-vars
const { amount, behavior } = lookupRelative(relValue)

switch (relUnit.toLowerCase()) {
case 'sec':
case 'secs':
case 'second':
case 'seconds':
this.rs += amount
break
case 'min':
case 'mins':
case 'minute':
case 'minutes':
this.ri += amount
break
case 'hour':
case 'hours':
this.rh += amount
break
case 'day':
case 'days':
this.rd += amount
break
case 'fortnight':
case 'fortnights':
case 'forthnight':
case 'forthnights':
this.rd += amount * 14
break
case 'week':
case 'weeks':
this.rd += amount * 7
break
case 'month':
case 'months':
this.rm += amount
break
case 'year':
case 'years':
this.ry += amount
break
case 'mon': case 'monday':
case 'tue': case 'tuesday':
case 'wed': case 'wednesday':
case 'thu': case 'thursday':
case 'fri': case 'friday':
case 'sat': case 'saturday':
case 'sun': case 'sunday':
this.resetTime()
this.weekday = lookupWeekday(relUnit, 7)
this.weekdayBehavior = 1
this.rd += (amount > 0 ? amount - 1 : amount) * 7
break
case 'weekday':
case 'weekdays':
// todo
break
}
}
},

relative: {
regex: RegExp('^([+-]*)[ \\t]*(\\d+)' + reSpaceOpt + '(' + reReltextunit + '|week)', 'i'),
name: 'relative',
callback (match, signs, relValue, relUnit) {
const minuses = signs.replace(/[^-]/g, '').length

let amount = +relValue * Math.pow(-1, minuses)

switch (relUnit.toLowerCase()) {
case 'sec':
case 'secs':
case 'second':
case 'seconds':
this.rs += amount
break
case 'min':
case 'mins':
case 'minute':
case 'minutes':
this.ri += amount
break
case 'hour':
case 'hours':
this.rh += amount
break
case 'day':
case 'days':
this.rd += amount
break
case 'fortnight':
case 'fortnights':
case 'forthnight':
case 'forthnights':
this.rd += amount * 14
break
case 'week':
case 'weeks':
this.rd += amount * 7
break
case 'month':
case 'months':
this.rm += amount
break
case 'year':
case 'years':
this.ry += amount
break
case 'mon': case 'monday':
case 'tue': case 'tuesday':
case 'wed': case 'wednesday':
case 'thu': case 'thursday':
case 'fri': case 'friday':
case 'sat': case 'saturday':
case 'sun': case 'sunday':
this.resetTime()
this.weekday = lookupWeekday(relUnit, 7)
this.weekdayBehavior = 1
this.rd += (amount > 0 ? amount - 1 : amount) * 7
break
case 'weekday':
case 'weekdays':
// todo
break
}
}
},

dayText: {
regex: RegExp('^(' + reDaytext + ')', 'i'),
name: 'daytext',
callback (match, dayText) {
this.resetTime()
this.weekday = lookupWeekday(dayText, 0)

if (this.weekdayBehavior !== 2) {
this.weekdayBehavior = 1
}
}
},

relativeTextWeek: {
regex: RegExp('^(' + reReltexttext + ')' + reSpace + 'week', 'i'),
name: 'relativetextweek',
callback (match, relText) {
this.weekdayBehavior = 2

switch (relText.toLowerCase()) {
case 'this':
this.rd += 0
break
case 'next':
this.rd += 7
break
case 'last':
case 'previous':
this.rd -= 7
break
}

if (isNaN(this.weekday)) {
this.weekday = 1
}
}
},

monthFullOrMonthAbbr: {
regex: RegExp('^(' + reMonthFull + '|' + reMonthAbbr + ')', 'i'),
name: 'monthfull | monthabbr',
callback (match, month) {
return this.ymd(this.y, lookupMonth(month), this.d)
}
},

tzCorrection: {
regex: RegExp('^' + reTzCorrection, 'i'),
name: 'tzcorrection',
callback (tzCorrection) {
return this.zone(processTzCorrection(tzCorrection))
}
},

ago: {
regex: /^ago/i,
name: 'ago',
callback () {
this.ry = -this.ry
this.rm = -this.rm
this.rd = -this.rd
this.rh = -this.rh
this.ri = -this.ri
this.rs = -this.rs
this.rf = -this.rf
}
},

gnuNoColon2: {
// second instance of gnunocolon, without leading 't'
// it's down here, because it is very generic (4 digits in a row)
// thus conflicts with many rules above
// only year4 should come afterwards
regex: RegExp('^' + reHour24lz + reMinutelz, 'i'),
name: 'gnunocolon',
callback (match, hour, minute) {
return this.time(+hour, +minute, 0, this.f)
}
},

year4: {
regex: RegExp('^' + reYear4),
name: 'year4',
callback (match, year) {
this.y = +year
return true
}
},

whitespace: {
regex: /^[ .,\t]+/,
name: 'whitespace'
// do nothing
},

any: {
regex: /^[\s\S]+/,
name: 'any',
callback () {
return false
}
}
}

let resultProto = {
// date
y: NaN,
m: NaN,
d: NaN,
// time
h: NaN,
i: NaN,
s: NaN,
f: NaN,

// relative shifts
ry: 0,
rm: 0,
rd: 0,
rh: 0,
ri: 0,
rs: 0,
rf: 0,

// weekday related shifts
weekday: NaN,
weekdayBehavior: 0,

// first or last day of month
// 0 none, 1 first, -1 last
firstOrLastDayOfMonth: 0,

// timezone correction in minutes
z: NaN,

// counters
dates: 0,
times: 0,
zones: 0,

// helper functions
ymd (y, m, d) {
if (this.dates > 0) {
return false
}

this.dates++
this.y = y
this.m = m
this.d = d
return true
},

time (h, i, s, f) {
if (this.times > 0) {
return false
}

this.times++
this.h = h
this.i = i
this.s = s
this.f = f

return true
},

resetTime () {
this.h = 0
this.i = 0
this.s = 0
this.f = 0
this.times = 0

return true
},

zone (minutes) {
if (this.zones <= 1) {
this.zones++
this.z = minutes
return true
}

return false
},

toDate (relativeTo) {
if (this.dates && !this.times) {
this.h = this.i = this.s = this.f = 0
}

// fill holes
if (isNaN(this.y)) {
this.y = relativeTo.getFullYear()
}

if (isNaN(this.m)) {
this.m = relativeTo.getMonth()
}

if (isNaN(this.d)) {
this.d = relativeTo.getDate()
}

if (isNaN(this.h)) {
this.h = relativeTo.getHours()
}

if (isNaN(this.i)) {
this.i = relativeTo.getMinutes()
}

if (isNaN(this.s)) {
this.s = relativeTo.getSeconds()
}

if (isNaN(this.f)) {
this.f = relativeTo.getMilliseconds()
}

// adjust special early
switch (this.firstOrLastDayOfMonth) {
case 1:
this.d = 1
break
case -1:
this.d = 0
this.m += 1
break
}

if (!isNaN(this.weekday)) {
var date = new Date(relativeTo.getTime())
date.setFullYear(this.y, this.m, this.d)
date.setHours(this.h, this.i, this.s, this.f)

var dow = date.getDay()

if (this.weekdayBehavior === 2) {
// To make "this week" work, where the current day of week is a "sunday"
if (dow === 0 && this.weekday !== 0) {
this.weekday = -6
}

// To make "sunday this week" work, where the current day of week is not a "sunday"
if (this.weekday === 0 && dow !== 0) {
this.weekday = 7
}

this.d -= dow
this.d += this.weekday
} else {
var diff = this.weekday - dow

// some PHP magic
if ((this.rd < 0 && diff < 0) || (this.rd >= 0 && diff <= -this.weekdayBehavior)) {
diff += 7
}

if (this.weekday >= 0) {
this.d += diff
} else {
this.d -= (7 - (Math.abs(this.weekday) - dow))
}

this.weekday = NaN
}
}

// adjust relative
this.y += this.ry
this.m += this.rm
this.d += this.rd

this.h += this.rh
this.i += this.ri
this.s += this.rs
this.f += this.rf

this.ry = this.rm = this.rd = 0
this.rh = this.ri = this.rs = this.rf = 0

let result = new Date(relativeTo.getTime())
// since Date constructor treats years <= 99 as 1900+
// it can't be used, thus this weird way
result.setFullYear(this.y, this.m, this.d)
result.setHours(this.h, this.i, this.s, this.f)

// note: this is done twice in PHP
// early when processing special relatives
// and late
// todo: check if the logic can be reduced
// to just one time action
switch (this.firstOrLastDayOfMonth) {
case 1:
result.setDate(1)
break
case -1:
result.setMonth(result.getMonth() + 1, 0)
break
}

// adjust timezone
if (!isNaN(this.z) && result.getTimezoneOffset() !== this.z) {
result.setUTCFullYear(
result.getFullYear(),
result.getMonth(),
result.getDate())

result.setUTCHours(
result.getHours(),
result.getMinutes() + this.z,
result.getSeconds(),
result.getMilliseconds())
}

return result
}
}

module.exports = function strtotime (str, now) {
// discuss at: http://locutus.io/php/strtotime/
// original by: Caio Ariede (http://caioariede.com)
// improved by: Kevin van Zonneveld (http://kvz.io)
// improved by: Caio Ariede (http://caioariede.com)
// improved by: A. Matías Quezada (http://amatiasq.com)
// improved by: preuter
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Mirko Faber
// input by: David
// bugfixed by: Wagner B. Soares
// bugfixed by: Artur Tchernychev
// bugfixed by: Stephan Bösch-Plepelits (http://github.com/plepe)
// reimplemented by: Rafał Kukawski
// note 1: Examples all have a fixed timestamp to prevent
// note 1: tests to fail because of variable time(zones)
// example 1: strtotime('+1 day', 1129633200)
// returns 1: 1129719600
// example 2: strtotime('+1 week 2 days 4 hours 2 seconds', 1129633200)
// returns 2: 1130425202
// example 3: strtotime('last month', 1129633200)
// returns 3: 1127041200
// example 4: strtotime('2009-05-04 08:30:00+00')
// returns 4: 1241425800
// example 5: strtotime('2009-05-04 08:30:00+02:00')
// returns 5: 1241418600
if (now == null) {
now = Math.floor(Date.now() / 1000)
}

// the rule order is very fragile
// as many formats are similar to others
// so small change can cause
// input misinterpretation
const rules = [
formats.yesterday,
formats.now,
formats.noon,
formats.midnightOrToday,
formats.tomorrow,
formats.timestamp,
formats.firstOrLastDay,
formats.backOrFrontOf,
// formats.weekdayOf, // not yet implemented
formats.mssqltime,
formats.timeLong12,
formats.timeShort12,
formats.timeTiny12,
formats.soap,
formats.wddx,
formats.exif,
formats.xmlRpc,
formats.xmlRpcNoColon,
formats.clf,
formats.iso8601long,
formats.dateTextual,
formats.pointedDate4,
formats.pointedDate2,
formats.timeLong24,
formats.dateNoColon,
formats.pgydotd,
formats.timeShort24,
formats.iso8601noColon,
// iso8601dateSlash needs to come before dateSlash
formats.iso8601dateSlash,
formats.dateSlash,
formats.american,
formats.americanShort,
formats.gnuDateShortOrIso8601date2,
formats.iso8601date4,
formats.gnuNoColon,
formats.gnuDateShorter,
formats.pgTextReverse,
formats.dateFull,
formats.dateNoDay,
formats.dateNoDayRev,
formats.pgTextShort,
formats.dateNoYear,
formats.dateNoYearRev,
formats.isoWeekDay,
formats.relativeText,
formats.relative,
formats.dayText,
formats.relativeTextWeek,
formats.monthFullOrMonthAbbr,
formats.tzCorrection,
formats.ago,
formats.gnuNoColon2,
formats.year4,
// note: the two rules below
// should always come last
formats.whitespace,
formats.any
]

let result = Object.create(resultProto)

while (str.length) {
for (let i = 0, l = rules.length; i < l; i++) {
const format = rules[i]

const match = str.match(format.regex)

if (match) {
// care only about false results. Ignore other values
if (format.callback && format.callback.apply(result, match) === false) {
return false
}

str = str.substr(match[0].length)
break
}
}
}

return Math.floor(result.toDate(new Date(now * 1000)) / 1000)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/datetime/strtotime'). You could also require the datetime module in full so that you could access datetime.strtotime instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Examples all have a fixed timestamp to prevent tests to fail because of variable time(zones)

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1strtotime('+1 day', 1129633200)1129719600
2strtotime('+1 week 2 days 4 hours 2 seconds', 1129633200)1130425202
3strtotime('last month', 1129633200)1127041200
4strtotime('2009-05-04 08:30:00+00')1241425800
5strtotime('2009-05-04 08:30:00+02:00')1241418600

« More PHP datetime functions