| | 198 | public static function getDateTimeForCulture($datetime, $culture) |
|---|
| | 199 | { |
|---|
| | 200 | if (!$datetime) return 0; |
|---|
| | 201 | |
|---|
| | 202 | $dateFormat = new sfDateFormat($culture); |
|---|
| | 203 | $inputPattern = $dateFormat->getInputPattern('g'); |
|---|
| | 204 | |
|---|
| | 205 | // We construct the regexp based on date format |
|---|
| | 206 | $dateRegexp = preg_replace(array('/[dmy]+|h+/i' ), '(\d+)', $inputPattern); |
|---|
| | 207 | |
|---|
| | 208 | // We parse date format to see where things are (m, d, y) |
|---|
| | 209 | $a = array( |
|---|
| | 210 | 'd' => strpos($inputPattern, 'd'), |
|---|
| | 211 | 'm' => strpos($inputPattern, 'M'), |
|---|
| | 212 | 'y' => strpos($inputPattern, 'y'), |
|---|
| | 213 | 'h' => strpos($inputPattern, 'H'), |
|---|
| | 214 | 'i' => strpos($inputPattern, 'm'), |
|---|
| | 215 | ); |
|---|
| | 216 | |
|---|
| | 217 | $tmp = array_flip($a); |
|---|
| | 218 | ksort($tmp); |
|---|
| | 219 | $i = 0; |
|---|
| | 220 | $c = array(); |
|---|
| | 221 | foreach ($tmp as $value) $c[++$i] = $value; |
|---|
| | 222 | $datePositions = array_flip($c); |
|---|
| | 223 | |
|---|
| | 224 | // We find all elements |
|---|
| | 225 | if (preg_match("~$dateRegexp~", $datetime, $matches)) |
|---|
| | 226 | { |
|---|
| | 227 | // We get matching timestamp |
|---|
| | 228 | return array( |
|---|
| | 229 | $matches[$datePositions['d']], |
|---|
| | 230 | $matches[$datePositions['m']], |
|---|
| | 231 | $matches[$datePositions['y']], |
|---|
| | 232 | $matches[$datePositions['h']], |
|---|
| | 233 | $matches[$datePositions['i']], |
|---|
| | 234 | ); |
|---|
| | 235 | } |
|---|
| | 236 | else |
|---|
| | 237 | { |
|---|
| | 238 | return null; |
|---|
| | 239 | } |
|---|
| | 240 | } |
|---|
| | 241 | |
|---|
| | 242 | // Return timestamp from a date formatted with a given culture |
|---|
| | 243 | public static function getDateTimeTimestampForCulture($date, $culture) |
|---|
| | 244 | { |
|---|
| | 245 | list($d, $m, $y, $h, $i) = self::getDateTimeForCulture($date, $culture); |
|---|
| | 246 | return mktime($h, $i, 0, $m, $d, $y); |
|---|
| | 247 | } |
|---|