peeaitchpee Posted April 7, 2009 Share Posted April 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/ Share on other sites More sharing options...
phil88 Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-803886 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-803890 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2009 Share Posted April 7, 2009 How about invalid dates? They might return a zero (as in January 1 1970 00:00:00 UTC). Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-803893 Share on other sites More sharing options...
peeaitchpee Posted April 7, 2009 Author Share Posted April 7, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-803987 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-803995 Share on other sites More sharing options...
peeaitchpee Posted April 7, 2009 Author Share Posted April 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804020 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 They will all return the same thing, I believe the standard, and what I've always used, is $arr['name']; Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804027 Share on other sites More sharing options...
killah Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804031 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804032 Share on other sites More sharing options...
peeaitchpee Posted April 7, 2009 Author Share Posted April 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804047 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804049 Share on other sites More sharing options...
thebadbad Posted April 7, 2009 Share Posted April 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804068 Share on other sites More sharing options...
Maq Posted April 7, 2009 Share Posted April 7, 2009 So, to answer your question, use single quotes inside the associative array, unless it's an index (number). Quote Link to comment https://forums.phpfreaks.com/topic/153056-strtotime-will-not-return-false/#findComment-804069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.