Jump to content

strtotime will not return false


peeaitchpee

Recommended Posts

I'm wanting to use strtotime to validate a date in a form, however it will not find it FALSE, ever. Everything passes.

 

Here is what I am using:

 

if (strtotime($_POST[date]) == FALSE){

// no good, enter it again

} else {

// we're good, do something

}

 

any suggestion? Thanks in advance

 

p.s. I'm using php 5.2

Link to comment
Share on other sites

The way I tend to check bugs with my code that are like this is to out put both things I'm trying to compare. In this case, one of those things is obviously False. So, put;

 

var_dump(strtotime($_POST['date']));

 

before your if statement and see what comes out. If it's not what you're expecting, it's probably something wrong with $_POST['date'], ie, it doesn't contain what you're expecting it to - so try outputting that too.

 

 

As far as syntax and logic goes, your code looks fine to me.

 

Edit:

Try using the strict comparison operator (===), might make a difference.

Link to comment
Share on other sites

Can you post an example of a value that you test in which you expect strtotime() to fail and return FALSE?

 

Because if you're using 5.1 or above it should return FALSE on failure, while previous versions still return -1.

 

I tested with this and I get the expected output: "no good"

 

$d = "blahuasldfjlasjfd";

if (strtotime($d) == FALSE){
// no good, enter it again
echo "no good";
} else {
// we're good, do something
echo "good";
}

?>

Link to comment
Share on other sites

wow..guy goes to lunch and finds all these replies. Thanks all.

 

to phil: i actually did do that, and the strange this is that it is passing over the $_POST['date'] as needed., hence why I was stumped. I also did the strict === and the result was the same

 

to Maq: that's why I put in the p.s. in my first post...am using 5.2. That said, when I modify my original

 

if (strtotime($_POST[date]) == FALSE){

// no good, enter it again

} else {

// we're good, do something

}

 

to

 

$date = $_POST[date];

 

if (strtotime($date == FALSE){

// no good, enter it again

} else {

// we're good, do something

}

 

using your method it works. That must mean there is something amiss in the php settings, since it should not make any differnce. Unless I'm completely nuts...

 

 

 

Link to comment
Share on other sites

to Maq: that's why I put in the p.s. in my first post...am using 5.2. That said, when I modify my original

 

I know you put that in your post, I was just stating what it said in the manual, I wasn't implying you had

 

Second, that's not how I had my code.

 

This line should throw an error, or maybe return FALSE, idk, but it wont' work properly:

 

if (strtotime($date == FALSE){ 

 

Can you post sample data where the code doesn't work as expected?  And the exact code that you're using.

Link to comment
Share on other sites

I hate forums sometime...never can get the person's tone of voice ;)

 

as far as the code i'm running, I posted it all - that's as far as I got on that file

 

there is a form on another file which has event name, location and date fields and I'm working on validating the post results.

 

$d = "blahuasldfjlasjfd";

 

if (strtotime($d) == FALSE){

// no good, enter it again

echo "no good";

} else {

// we're good, do something

echo "good";

}

 

is what you had.

 

so changed the $d to $date.

 

and declared $date = $_POST[date] as opposed to "blahuasldfjlasjfd"

 

Maybe I fail to see the difference. Regardless I did get it working, so thank you.

 

This leads me to another question...

 

$_POST["date"], $_POST['date'] and $_POST[date] are not the same, are they? I was always under the impression that it was just a preference in syntax. That's not the case is it? What is the difference and/or where should I look it up in the docs?

 

Thanks again

Link to comment
Share on other sites

I read this up some where.

 

Using $arr[name] expect's it to be a number and look's for a number. Causing more page load since it is not really a number.

Using $arr['name'] i suggest is the best way to go since it performs faster as all single quotes perform milliseconds faster than double quotes.

Link to comment
Share on other sites

I read this up some where.

 

Using $arr[name] expect's it to be a number and look's for a number. Causing more page load since it is not really a number.

Using $arr['name'] i suggest is the best way to go since it performs faster as all single quotes perform milliseconds faster than double quotes.

 

Sounds right to me, makes sense too, thanks for the info  ;)

Link to comment
Share on other sites

ok..so feel free to rip me...

 

how would you syntax this?:

 

$sql = "SELECT `id`, `link`, `topic`, `copy` FROM `newsletter_copy` WHERE `newsletter` = '$_POST[newsletter]' ORDER BY `order` ASC";

 

this does work...but if I'm reading right...I'm been doing it wrong forever

Link to comment
Share on other sites

Good question, like this:

 

$sql = "SELECT `id`, `link`, `topic`, `copy` FROM `newsletter_copy` WHERE `newsletter` = '{$_REQUEST['newsletter']}' ORDER BY `order` ASC";

 

EDIT: Although, the way you had it should still work.

Link to comment
Share on other sites

newsletter inside the square brackets in your example is a string (key in the array), therefore it should be quoted. When it's not quoted, PHP thinks it's a constant, finds out it's not defined, and then assumes it's a string and issues a notice/warning. It's only because PHP is a pretty loose language that you won't get a fatal error when forgetting the quotes ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.