├── README.md └── function.find_date.php /README.md: -------------------------------------------------------------------------------- 1 | Find Date in String 2 | ======================= 3 | 4 | Function to find a date in a string using Regular Expression 5 | 6 | 7 | ### Usage 8 | 9 | $string = "This building was cleaned on the 8th of October 2006 after a huge storm." 10 | $date = find_date( $string ); 11 | 12 | // Return: 13 | array( 14 | 'day' => 08, 15 | 'month' => 10, 16 | 'year' => 2006 17 | ); 18 | 19 | 20 | ### Other examples: 21 | 22 | * 01/01/2012 will return > 01-01-2012 23 | * 30-12-11 will return > 30-12-2011 24 | * 1 2 1985 will return > 01-02-1985 25 | * 10.12.1850 will return > 10-12-1850 26 | * 5 12 85 will return > 05-12-1985 27 | * 5 October 2012 will return > 02-10-2012 28 | * MAY 5th 1985 will return > 05-05-1985 29 | * 1st March 81 will return > 01-03-1981 30 | * March 1st 2015 return > 01-03-2015 31 | * Apr 20 2000 return > 20-04-2000 32 | * Sunday, 24 June 1999 return > 24-06-1999 33 | * Mon 2nd July 1995 return > 02-07-1995 34 | -------------------------------------------------------------------------------- /function.find_date.php: -------------------------------------------------------------------------------- 1 | 01, 'month' => 01, 'year' => 2012 ) 12 | */ 13 | function find_date( $string ) { 14 | $shortenize = function( $string ) { 15 | return substr( $string, 0, 3 ); 16 | }; 17 | 18 | // Define month name: 19 | $month_names = array( 20 | "january", 21 | "february", 22 | "march", 23 | "april", 24 | "may", 25 | "june", 26 | "july", 27 | "august", 28 | "september", 29 | "october", 30 | "november", 31 | "december" 32 | ); 33 | $short_month_names = array_map( $shortenize, $month_names ); 34 | 35 | // Define day name 36 | $day_names = array( 37 | "monday", 38 | "tuesday", 39 | "wednesday", 40 | "thursday", 41 | "friday", 42 | "saturday", 43 | "sunday" 44 | ); 45 | $short_day_names = array_map( $shortenize, $day_names ); 46 | 47 | // Define ordinal number 48 | $ordinal_number = ['st', 'nd', 'rd', 'th']; 49 | 50 | $day = ""; 51 | $month = ""; 52 | $year = ""; 53 | 54 | // Match dates: 01/01/2012 or 30-12-11 or 1 2 1985 55 | preg_match( '/([0-9]?[0-9])[\.\-\/ ]+([0-1]?[0-9])[\.\-\/ ]+([0-9]{2,4})/', $string, $matches ); 56 | if ( $matches ) { 57 | if ( $matches[1] ) 58 | $day = $matches[1]; 59 | if ( $matches[2] ) 60 | $month = $matches[2]; 61 | if ( $matches[3] ) 62 | $year = $matches[3]; 63 | } 64 | 65 | // Match dates: Sunday 1st March 2015; Sunday, 1 March 2015; Sun 1 Mar 2015; Sun-1-March-2015 66 | 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 ); 67 | if ( $matches ) { 68 | if ( empty( $day ) && $matches[1] ) 69 | $day = $matches[1]; 70 | 71 | if ( empty( $month ) && $matches[2] ) { 72 | $month = array_search( strtolower( $matches[2] ), $short_month_names ); 73 | 74 | if ( ! $month ) 75 | $month = array_search( strtolower( $matches[2] ), $month_names ); 76 | 77 | $month = $month + 1; 78 | } 79 | 80 | if ( empty( $year ) && $matches[3] ) 81 | $year = $matches[3]; 82 | } 83 | 84 | // Match dates: March 1st 2015; March 1 2015; March-1st-2015 85 | preg_match('/(' . implode( '|', $month_names ) . '|' . implode( '|', $short_month_names ) . ')[ ,\-_\/]*([0-9]?[0-9])[ ,\-_\/]*(?:' . implode( '|', $ordinal_number ) . ')?[ ,\-_\/]+([0-9]{4})/i', $string, $matches ); 86 | if ( $matches ) { 87 | if ( empty( $month ) && $matches[1] ) { 88 | $month = array_search( strtolower( $matches[1] ), $short_month_names ); 89 | 90 | if ( ! $month ) 91 | $month = array_search( strtolower( $matches[1] ), $month_names ); 92 | 93 | $month = $month + 1; 94 | } 95 | 96 | if ( empty( $day ) && $matches[2] ) 97 | $day = $matches[2]; 98 | 99 | if ( empty( $year ) && $matches[3] ) 100 | $year = $matches[3]; 101 | } 102 | 103 | // Match month name: 104 | if ( empty( $month ) ) { 105 | preg_match( '/(' . implode( '|', $month_names ) . ')/i', $string, $matches_month_word ); 106 | if ( $matches_month_word && $matches_month_word[1] ) 107 | $month = array_search( strtolower( $matches_month_word[1] ), $month_names ); 108 | 109 | // Match short month names 110 | if ( empty( $month ) ) { 111 | preg_match( '/(' . implode( '|', $short_month_names ) . ')/i', $string, $matches_month_word ); 112 | if ( $matches_month_word && $matches_month_word[1] ) 113 | $month = array_search( strtolower( $matches_month_word[1] ), $short_month_names ); 114 | } 115 | 116 | $month = $month + 1; 117 | } 118 | 119 | // Match 5th 1st day: 120 | if ( empty( $day ) ) { 121 | preg_match( '/([0-9]?[0-9])(' . implode( '|', $ordinal_number ) . ')/', $string, $matches_day ); 122 | if ( $matches_day && $matches_day[1] ) 123 | $day = $matches_day[1]; 124 | } 125 | 126 | // Match Year if not already setted: 127 | if ( empty( $year ) ) { 128 | preg_match( '/[0-9]{4}/', $string, $matches_year ); 129 | if ( $matches_year && $matches_year[0] ) 130 | $year = $matches_year[0]; 131 | } 132 | if ( ! empty ( $day ) && ! empty ( $month ) && empty( $year ) ) { 133 | preg_match( '/[0-9]{2}/', $string, $matches_year ); 134 | if ( $matches_year && $matches_year[0] ) 135 | $year = $matches_year[0]; 136 | } 137 | 138 | // Day leading 0 139 | if ( 1 == strlen( $day ) ) 140 | $day = '0' . $day; 141 | 142 | // Month leading 0 143 | if ( 1 == strlen( $month ) ) 144 | $month = '0' . $month; 145 | 146 | // Check year: 147 | if ( 2 == strlen( $year ) && $year > 20 ) 148 | $year = '19' . $year; 149 | else if ( 2 == strlen( $year ) && $year < 20 ) 150 | $year = '20' . $year; 151 | 152 | $date = array( 153 | 'year' => $year, 154 | 'month' => $month, 155 | 'day' => $day 156 | ); 157 | 158 | // Return false if nothing found: 159 | if ( empty( $year ) && empty( $month ) && empty( $day ) ) 160 | return false; 161 | else 162 | return $date; 163 | } 164 | ?> 165 | --------------------------------------------------------------------------------