note 51215 deleted from function.date by philip

From: Date: Mon, 11 Apr 2005 16:22:45 +0000
Subject: note 51215 deleted from function.date by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-87848@lists.php.net to get a copy of this message
Note Submitter: kasper at johansen dot tdcadsl dot dk ---- I could not find any functions to test "text-patterns" as dates, so I wrote one myself. When I say text-patterns I mean finding out which pattern a date uses, and getting the date back in Unixstamp-format. I just added some patterns which I used, but one should be able to add more, since it is very easy (the $date-array). I used this function to test dates in incomming text-files, and validated all the cells (the cells was seperated with ";"). Then I could find out which field was dates, and which date-format they used. If anyone out there so get into the same problem they can maybe use what I came made: <? $date[] = "yyyymmdd"; $date[] = "dd-mm-yy"; //$date[] = "dd/mm/yy"; // This wont be needed, since the "/" and "-" will be treated as nothing. foreach($date AS $pattern){ $month_w = strpos($pattern, "mm"); $month = substr($cell, $month_w, 2); if (strpos($pattern, "yyyy") !== false){ $year_w = strpos($pattern, "yyyy"); $year_f = 4; }elseif(strpos($pattern, "yy") !== false){ $tests_count++; $year_w = strpos($pattern, "yy"); $year_f = 2; } $dato_w = strpos($pattern, "dd"); $dato = substr($cell, $dato_w, 2); $insert[dato_w] = $dato_w; $insert[month_w] = $month_w; $insert[year_w] = $year_w; $insert[year_f] = $year_f; $insert[pattern] = $pattern; $date_pattern[] = $insert; } $cell = "17-06-85"; // My birthday $valid = false; foreach($date_pattern AS $pattern){ $cell = trim($cell); if ($cell){ if (strlen($cell) == strlen($pattern[pattern])){ $dato = substr($cell, $pattern[dato_w], 2); $month = substr($cell, $pattern[month_w], 2); $year = substr($cell, $pattern[year_w], $pattern[year_f]); if ( is_numeric($dato) && $dato > 0 && $dato <= 31 && is_numeric($month) && $month > 0 && $month <= 12 && is_numeric($year) && $year > 0 ){ $unix = mktime(0, 0, 0, $month, $dato, $year); // I knew that in my work the dates would be from 1990 and above, so it would be safe to do this: if ($unix > 0){ // Is it valid? $valid = true; // Which pattern was it? $valid_pattern = $pattern[pattern]; } } } } } // This example is put together from a lot of other code (I removed the other code, since it would // be irrelevant to the example and function), and I have not tested it. // // But one should be able to get the idea while reading it anyway ;) And i added some comments too ;) ?>

« previous php.notes (#87848) next »