Check if variable is a date
Moderator: General Moderators
- noguru
- Forum Commoner
- Posts: 61
- Joined: Thu Jun 06, 2002 4:03 am
- Location: Just north of the City Of Gold, Land of Milk and Honey
Check if variable is a date
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.
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.
are there any other locations in this file where 8 numbers will be stored consecutively? if not, use
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).
Code: Select all
if(ereg("її:digit:]]{8}", $stringtosearch, $result)
$date = $resultї0];
else
echo "no date found"- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
this is off topic, but where can i read up on how to use all of the [[: thing :]]{x} ... regular expressionsllimllib wrote:are there any other locations in this file where 8 numbers will be stored consecutively? if not, useIf 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).Code: Select all
if(ereg("її:digit:]]{8}", $stringtosearch, $result) $date = $resultї0]; else echo "no date found"
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
If you're expecting dates in yyyymmdd format then you can work with them like this:
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
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';
}
}
?>Mac
Here's a safer way to check for a date. This will never return a date more than or less than 8 digits.
Or if you need the year, month, and day:
If you need 100% accuracy, use the second method and go with checkdate():
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.";
?>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.";
?>Code: Select all
checkdate($month_found, $day_found, $year_found)