forked from etiennetremel/PHP-Find-Date-in-String
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.find_date.php
164 lines (139 loc) · 4.71 KB
/
function.find_date.php
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
<?php
/**
* Find Date in a String
*
* @author Etienne Tremel
* @license http://creativecommons.org/licenses/by/3.0/ CC by 3.0
* @link http://www.etiennetremel.net
* @version 0.2.0
*
* @param string find_date( ' some text 01/01/2012 some text' ) or find_date( ' some text October 5th 86 some text' )
* @return mixed false if no date found else array: array( 'day' => 01, 'month' => 01, 'year' => 2012 )
*/
function find_date( $string ) {
$shortenize = function( $string ) {
return substr( $string, 0, 3 );
};
// Define month name:
$month_names = array(
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december"
);
$short_month_names = array_map( $shortenize, $month_names );
// Define day name
$day_names = array(
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday"
);
$short_day_names = array_map( $shortenize, $day_names );
// Define ordinal number
$ordinal_number = ['st', 'nd', 'rd', 'th'];
$day = "";
$month = "";
$year = "";
// Match dates: 01/01/2012 or 30-12-11 or 1 2 1985
preg_match( '/([0-9]?[0-9])[\.\-\/ ]+([0-1]?[0-9])[\.\-\/ ]+([0-9]{2,4})/', $string, $matches );
if ( $matches ) {
if ( $matches[1] )
$day = $matches[1];
if ( $matches[2] )
$month = $matches[2];
if ( $matches[3] )
$year = $matches[3];
}
// Match dates: Sunday 1st March 2015; Sunday, 1 March 2015; Sun 1 Mar 2015; Sun-1-March-2015
preg_match('/(?:(?:' . implode( '|', $day_names ) . '|' . implode( '|', $short_day_names ) . ')[ ,\-_\/]*)?([0-9]?[0-9])[ ,\-_\/]*(?:' . implode( '|', $ordinal_number ) . ')?[ ,\-_\/]*(' . implode( '|', $month_names ) . '|' . implode( '|', $short_month_names ) . ')[ ,\-_\/]+([0-9]{4})/i', $string, $matches );
if ( $matches ) {
if ( empty( $day ) && $matches[1] )
$day = $matches[1];
if ( empty( $month ) && $matches[2] ) {
$month = array_search( strtolower( $matches[2] ), $short_month_names );
if ( ! $month )
$month = array_search( strtolower( $matches[2] ), $month_names );
$month = $month + 1;
}
if ( empty( $year ) && $matches[3] )
$year = $matches[3];
}
// Match dates: March 1st 2015; March 1 2015; March-1st-2015
preg_match('/(' . implode( '|', $month_names ) . '|' . implode( '|', $short_month_names ) . ')[ ,\-_\/]*([0-9]?[0-9])[ ,\-_\/]*(?:' . implode( '|', $ordinal_number ) . ')?[ ,\-_\/]+([0-9]{4})/i', $string, $matches );
if ( $matches ) {
if ( empty( $month ) && $matches[1] ) {
$month = array_search( strtolower( $matches[1] ), $short_month_names );
if ( ! $month )
$month = array_search( strtolower( $matches[1] ), $month_names );
$month = $month + 1;
}
if ( empty( $day ) && $matches[2] )
$day = $matches[2];
if ( empty( $year ) && $matches[3] )
$year = $matches[3];
}
// Match month name:
if ( empty( $month ) ) {
preg_match( '/(' . implode( '|', $month_names ) . ')/i', $string, $matches_month_word );
if ( $matches_month_word && $matches_month_word[1] )
$month = array_search( strtolower( $matches_month_word[1] ), $month_names );
// Match short month names
if ( empty( $month ) ) {
preg_match( '/(' . implode( '|', $short_month_names ) . ')/i', $string, $matches_month_word );
if ( $matches_month_word && $matches_month_word[1] )
$month = array_search( strtolower( $matches_month_word[1] ), $short_month_names );
}
$month = $month + 1;
}
// Match 5th 1st day:
if ( empty( $day ) ) {
preg_match( '/([0-9]?[0-9])(' . implode( '|', $ordinal_number ) . ')/', $string, $matches_day );
if ( $matches_day && $matches_day[1] )
$day = $matches_day[1];
}
// Match Year if not already setted:
if ( empty( $year ) ) {
preg_match( '/[0-9]{4}/', $string, $matches_year );
if ( $matches_year && $matches_year[0] )
$year = $matches_year[0];
}
if ( ! empty ( $day ) && ! empty ( $month ) && empty( $year ) ) {
preg_match( '/[0-9]{2}/', $string, $matches_year );
if ( $matches_year && $matches_year[0] )
$year = $matches_year[0];
}
// Day leading 0
if ( 1 == strlen( $day ) )
$day = '0' . $day;
// Month leading 0
if ( 1 == strlen( $month ) )
$month = '0' . $month;
// Check year:
if ( 2 == strlen( $year ) && $year > 20 )
$year = '19' . $year;
else if ( 2 == strlen( $year ) && $year < 20 )
$year = '20' . $year;
$date = array(
'year' => $year,
'month' => $month,
'day' => $day
);
// Return false if nothing found:
if ( empty( $year ) && empty( $month ) && empty( $day ) )
return false;
else
return $date;
}
?>