1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: /**
15: * Validator checking if the input value is valid date and time in YYYY-MM-DD HH::MM::SS format.
16: *
17: * @category Jyxo
18: * @package Jyxo_Input
19: * @subpackage Validator
20: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
21: * @license https://github.com/jyxo/php/blob/master/license.txt
22: * @author Jan Pěček
23: */
24: class Jyxo_Input_Validator_IsDateTime extends Jyxo_Input_Validator_AbstractValidator
25: {
26: /**
27: * Validates a value.
28: *
29: * @param mixed $value Input value
30: * @return boolean
31: */
32: public function isValid($value)
33: {
34: // Format check
35: if (!preg_match('~^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$~', (string) $value, $matches)) {
36: return false;
37: }
38:
39: list(, $year, $month, $day, $hour, $minute, $second) = $matches;
40:
41: // Date validity check
42: if (!checkdate($month, $day, $year)) {
43: return false;
44: }
45:
46: // Time validity check
47: if ($hour < 0 || $hour > 23) {
48: return false;
49: }
50: if ($minute < 0 || $minute > 59) {
51: return false;
52: }
53: if ($second < 0 || $second > 59) {
54: return false;
55: }
56:
57: return true;
58: }
59: }
60: