note 72958 added to function.date
| From: | EamonStraughneamon at gizzle dot co dot uk | Date: | Mon, 05 Feb 2007 21:33:12 +0000 |
| Subject: | note 72958 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122898@lists.php.net to get a copy of this message | ||
Check this out this is a class i made last year for Gizzle.co.uk but sinced used on other projects
and is quite a treat. It checks for a absolute valid date or datetime type. returns booleen.
Disclaimer I do not take responsibility for the use of this class. I guarantees it works.
<?php
#Created By Eamon Straughn, Class extends Date() informally
class isDate
{
private $arg;
private $return = false;
private $stack;
private $stacks;
private $pos = array();
private $poss = array();
public function __construct($arg)
{
$this->arg = trim($arg);
}
private function DateType()
{
//if length is more than 10 then it is not a date
if(($n = strlen($this->arg)) > 10)
return false;
//if length is less than 6 then it is not a date
elseif($n < 6)
return false;
//get values into arrays for processing
for($i = 0; $i < $n; $i ++)
{
if(is_numeric($this->arg[$i]))
$this->stack .= (isset($this->arg[$i])) ? $this->arg[$i] : null;
elseif(($this->arg[$i] == "-") || ($this->arg[$i] == "/") ||
($this->arg[$i] == "\\"))
$this->pos[$i] = (isset($i)) ? $i : null;
}
//strip values to be checked
if(is_numeric($c = substr($this->stack, 0, current($this->pos))))
$d = (strlen($c) > 2) ? substr($this->stack, current($this->pos),
current($this->pos)/2) : substr($this->stack, current($this->pos), current($this->pos));
else
return false;
if(is_numeric($d))
if(is_numeric($e = substr($this->stack, end($this->pos)-1, end($this->pos)-1)));
//convert string to integer
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
//check if it is a valid date
if((checkdate($d,$c,$e)) || (checkdate($d,$e,$c)))
return true;
return false;
}
private function DateTimeType()
{
//if length is more than 10 then it is not a datetime
if(($n = strlen($this->arg)) < 10)
return false;
//if length is less than 19 then it is not a datetime
elseif($n < 19)
return false;
//get values into arrays for processing
for($i = 0; $i < $n; $i ++)
{
if(is_numeric($this->arg[$i]))
$this->stacks .= (isset($this->arg[$i])) ? $this->arg[$i] : null;
elseif(($this->arg[$i] == "-") || ($this->arg[$i] == "/") ||
($this->arg[$i] == "\\") || ($this->arg[$i] == " ") || ($this->arg[$i]
== ":"))
$this->poss[$i] = (isset($i)) ? $i : null;
}
//strip values to be checked
if(is_numeric($c = substr($this->stacks, 0, current($this->poss))))
$d = (strlen($c) > 2) ? substr($this->stacks, current($this->poss),
current($this->poss)/2) : substr($this->stacks, current($this->poss),
current($this->poss));
else
return false;
if(is_numeric($d))
$e = (strlen($c) > 2) ? substr($this->stacks, next($this->poss)-1,
(next($this->poss)-1)/4) : substr($this->stacks, next($this->poss)-1,
(next($this->poss)-1)/2);
if(is_numeric($e))
if(is_numeric($f = substr($this->stacks, (next($this->poss)/2)+2, 2)));
if(is_numeric($g = substr($this->stacks, (next($this->poss)/2)+2, 2)));
if(is_numeric($h = substr($this->stacks, (end($this->poss)/2)+4, 2)));
//convert string to integer
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;
$h = (int) $h;
//check if the time is valid
if($f > 24)
return false;
if($g > 60)
return false;
if($h > 60)
return false;
//check if it is a valid date
if((checkdate($d,$c,$e)) || (checkdate($d,$e,$c)))
return true;
return false;
}
public function DateCheck()
{
if($this->DateTimeType())
$this->return = true;
elseif($this->DateType())
$this->return = true;
else
$this->return = false;
return $this->return;
}
}
?>
example of use
<?php
//assign a date
$d = "2006-12-12 10:15:00"; $t = "12-12-2006"; $f = "1-1-2001
10:23:09";
//the above can be used regardless if it is shortdate or long date but remember the year must always
be 4 integers.
$c = new isDate($d);
if($c->CheckDate() !== false) echo $d;
?>
if anyone wants to extend it to check time and posibly do more feel free to.
Eamon..
----
Server IP: 85.116.0.130
Probable Submitter: 80.229.95.154
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/72958
Del: integrated -- https://master.php.net/note/delete/72958/integrated
Del: useless -- https://master.php.net/note/delete/72958/useless
Del: bad code -- https://master.php.net/note/delete/72958/bad+code
Del: spam -- https://master.php.net/note/delete/72958/spam
Del: non-english -- https://master.php.net/note/delete/72958/non-english
Del: in docs -- https://master.php.net/note/delete/72958/in+docs
Del: other reasons-- https://master.php.net/note/delete/72958
Reject -- https://master.php.net/note/reject/72958
Search -- https://master.php.net/manage/user-notes.php