Check if variable is a date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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

Post 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.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

best solution is to use a regular expression. What format is the date in?
User avatar
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

Post 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.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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).
User avatar
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

Post by noguru »

there shouldn't be, i'll just make sure.

thanks i'll try it.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There is of course also the checkdate() function.

Mac
User avatar
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

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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)) &#123;
    echo 'Please enter date in yyyymmdd format.';
&#125; else &#123;
    $year = substr($date, 0, 4);
    $month = substr($date, 4, 2);
    $day = substr($date, 6, 2);
    if (!checkdate($month, $day, $year)) &#123;
         echo 'Please enter a valid date';
    &#125; else &#123;
         echo 'Valid date';
    &#125;
&#125;
?>
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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

hob_goblin, there's a good article on evolt about regular expressions which explains all the character classes.
Ukrainian
Forum Newbie
Posts: 1
Joined: Thu Jul 11, 2002 9:44 am
Contact:

Post by Ukrainian »

checkdate
(PHP 3, PHP 4 )

checkdate -- Validate a gregorian date/time
Description
bool checkdate ( int month, int day, int year)
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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('/(&#1111;^0-9]|)(&#1111;21]&#1111;09]&#1111;0-9]&#1111;0-9]&#1111;01]&#1111;0-9]&#1111;0-3]&#1111;0-9])(&#1111;^0-9]|)/', $data_to_search, $matches))
&#123;
	print "And the date is...$matches&#1111;2]";
&#125;
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('/(&#1111;^0-9]|)(&#1111;21]&#1111;09]&#1111;0-9]&#1111;0-9])(&#1111;01]&#1111;0-9])(&#1111;0-3]&#1111;0-9])(&#1111;^0-9]|)/', $data_to_search, $matches))
&#123;
	$year_found = $matches&#1111;2];
	$month_found = $matches&#1111;3];
	$day_found = $matches&#1111;4];

	print "And the date is...$month_found/$day_found/$year_found";
&#125;
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)
Post Reply