Page 1 of 1
Check if variable is a date
Posted: Wed Jul 10, 2002 9:43 am
by noguru
hi
is there a function in php to check if a variable is a date? I want to do something like the following:
if (isdate($vrbl){
//do something
}else{
//do something else
}
Thanks.
Posted: Wed Jul 10, 2002 9:55 am
by llimllib
best solution is to use a regular expression. What format is the date in?
Posted: Wed Jul 10, 2002 10:01 am
by noguru
The date is in the format "20020709". I open a text file and loop through all the values and if a date is found, it must be manipulated further into seperate year, month and day elements.
Posted: Wed Jul 10, 2002 10:14 am
by llimllib
are there any other locations in this file where 8 numbers will be stored consecutively? if not, use
Code: Select all
if(ereg("її:digit:]]{8}", $stringtosearch, $result)
$date = $resultї0];
else
echo "no date found"
If there are other places in your file where anything *could* have 8 consecutive digits, you'll have to get more complex with this. If not, this should do (although i didn't test it).
Posted: Wed Jul 10, 2002 10:20 am
by noguru
there shouldn't be, i'll just make sure.
thanks i'll try it.
Posted: Thu Jul 11, 2002 1:37 am
by twigletmac
There is of course also the
checkdate() function.
Mac
Posted: Thu Jul 11, 2002 1:46 am
by noguru
I have already tried the checkdate() function but it expects 3 parameters: checkdate ( int month, int day, int year).
When I did a checkdate on "20020907", i got an error stating "wrong number of arguments" or something like that.
Posted: Thu Jul 11, 2002 1:50 am
by hob_goblin
llimllib wrote:are there any other locations in this file where 8 numbers will be stored consecutively? if not, use
Code: Select all
if(ereg("її:digit:]]{8}", $stringtosearch, $result)
$date = $resultї0];
else
echo "no date found"
If there are other places in your file where anything *could* have 8 consecutive digits, you'll have to get more complex with this. If not, this should do (although i didn't test it).
this is off topic, but where can i read up on how to use all of the [[: thing :]]{x} ... regular expressions
Posted: Thu Jul 11, 2002 2:09 am
by twigletmac
If you're expecting dates in yyyymmdd format then you can work with them like this:
Code: Select all
<?php
$date = '20020907';
if (strlen($date) != 8 && !is_numeric($date)) {
echo 'Please enter date in yyyymmdd format.';
} else {
$year = substr($date, 0, 4);
$month = substr($date, 4, 2);
$day = substr($date, 6, 2);
if (!checkdate($month, $day, $year)) {
echo 'Please enter a valid date';
} else {
echo 'Valid date';
}
}
?>
This'll check whether the input is long enough and a number and then check to make sure that the actual date is valid. Someone else may be able to offer something a bit more streamlined but I'd say the basic idea is there.
Mac
Posted: Thu Jul 11, 2002 8:22 am
by llimllib
hob_goblin, there's a good
article on evolt about regular expressions which explains all the character classes.
Posted: Thu Jul 11, 2002 9:44 am
by Ukrainian
checkdate
(PHP 3, PHP 4 )
checkdate -- Validate a gregorian date/time
Description
bool checkdate ( int month, int day, int year)
Posted: Fri Jul 12, 2002 2:23 am
by gnu2php
Here's a safer way to check for a date. This will never return a date more than or less than 8 digits.
Code: Select all
<?php // Searches for a date between 1900-00-00 and 2099-19-39
$data_to_search = 'Today is 20020907';
if (preg_match('/(ї^0-9]|)(ї21]ї09]ї0-9]ї0-9]ї01]ї0-9]ї0-3]ї0-9])(ї^0-9]|)/', $data_to_search, $matches))
{
print "And the date is...$matchesї2]";
}
else print "Sorry, no date found.";
?>
Or if you need the year, month, and day:
Code: Select all
<?php // Searches for a date between 19000000 and 20991939
$data_to_search = 'Today is 20020907';
if (preg_match('/(ї^0-9]|)(ї21]ї09]ї0-9]ї0-9])(ї01]ї0-9])(ї0-3]ї0-9])(ї^0-9]|)/', $data_to_search, $matches))
{
$year_found = $matchesї2];
$month_found = $matchesї3];
$day_found = $matchesї4];
print "And the date is...$month_found/$day_found/$year_found";
}
else print "Sorry, no date found.";
?>
If you need 100% accuracy, use the second method and go with checkdate():
Code: Select all
checkdate($month_found, $day_found, $year_found)