Thanks Adam, I had sent out my second email before I had read yours. I'll give yours a go, thanks again. Mark -----Original Message----- From: Adam Zey [mailto:azey@xxxxxx] Sent: Wednesday, August 02, 2006 11:15 AM To: Mark Steudel Cc: PHP Mailing Lists Subject: Re: date(n/Y) strtotime Mark Steudel wrote: > In the strtotime notes, it says that strtotime returns -1 previous to > php5, but if I do: > > If( strtotime( '1/2009') == -1 ) > { > Echo 'false'; > } > Else > { > Echo 'true'; > } > > If( strtotime( '1/2009') === -1 ) > { > Echo 'false'; > } > Else > { > Echo 'true'; > } > > If( strtotime( '1/2009') == '-1' ) > { > Echo 'false'; > } > Else > { > Echo 'true'; > } > > > All of those echo true, how do I determine if strtotime has failed or > not? > > Mark > -----Original Message----- > From: Mark Steudel > Sent: Wednesday, August 02, 2006 9:55 AM > To: Mark Steudel; PHP Mailing Lists > Subject: RE: date(n/Y) strtotime > > Ok so actually I didn't solve it. Php5, this works, but php 4.4.1 and > 4.4.0 don't handle this correctly. Here's my code I'm running on each > box: > > function expDate2str( $date ) > { > if( !($sDate = strtotime( $date ) ) ) > { > echo "Invalid, blowing up date<br />"; > > // exploded date > $eDate = explode( '/', $date ); > > // string date > $sDate = strtotime( date( "Y-m-d", mktime( 0, 0, 0, > $eDate[0], 1, $eDate[1] ) ) ); > } > else > { > echo "valid<br/>"; > } > echo "In: " .$date ."<br />Out: ". date( "Y-m-d", $sDate ) ."<br > /><br />"; > } > > expDate2str('1/2009'); > expDate2str( date( "n/Y")); > > > Here are the results: > > Php 5.1.2 > > Invalid, blowing up date > In: 1/2009 > Out: 2009-01-01 > > Invalid, blowing up date > In: 8/2006 > Out: 2006-08-01 > > PHP 4.4.1 > > Valid > In: 1/2009 > Out: 2011-07-02 > > Valid > In: 8/2006 > Out: 2012-01-27 > > PHP 4.4.0 > Valid > In: 1/2009 > Out: 2011-07-02 > > Valid > In: 8/2006 > Out: 2012-01-27 > > Any work around with these types of dates on php4? > > Mark > -----Original Message----- > From: Mark Steudel > Sent: Wednesday, August 02, 2006 9:46 AM > To: PHP Mailing Lists > Subject: date(n/Y) strtotime > > I've always been really amazed at how well strtotime works, but recently > ran into an issue where it couldn't figure out the date if it was a cc > exp date in long format, e.g. 1/2009. I was curious if anyone else has > run into this and how did they get around it, here was my solution: > > function expDate2str( $date ) > { > if( !($sDate = strtotime( $date ) ) ) > { > // exploded date > $eDate = explode( '/', $date ); > > // string date we hard code the day to 1 > $sDate = strtotime( date( "Y-m-d", mktime( 0, 0, 0, > $eDate[0], 1, $eDate[1] ) ) ); > > > } > > return $sDate; > } > > Thanks, Mark > > -------------------------------------------------------------- > Mark Steudel > Web Applications Developer > 555 Dayton St > Suite A > Edmonds, WA 98020 > p: 425.741.7014 > e: mark@xxxxxxxxxxxx > w: http://www.netriver.net > Did you try my code? It's a lot simpler than yours, faster too. And I just tried executing it to confirm that it works. The problem with your comparison is that strtotime thinks the date is invalid, but misinterprets it. But since once you decide the date is invalid you assume it's in a given format, it is enough to simply check that it's in the certain format to begin with. I don't know how complex you want to get with this, here's a relatively simple if statement to do the check: if ( 6 <= strlen($date) <= 7 && substr_count($date, "/") == 1 ) Which checks that the length is from our credit card strings, and that it looks somewhat valid (You could go more in depth in checking, but this'll do for now). Now, that new comparison would be needed for your code, but I'll rewrite your function to use my simpler method of conversion: <?php function expDate2str($date) { $datelen = strlen($date); if ( ($datelen == 6 || $datelen == 7) && substr_count($date, "/") == 1 ) { echo "Invalid, blowing up date<br/>"; $sDate = strtotime(str_replace("/", "/01/", strlen($date) == 6 ? "0$date" : $date)); } else { echo "valid<br/>"; } echo "In: $date<br/>Out: ". date( "Y-m-d", $sDate ) ."<br/><br/>"; } expDate2str('1/2009'); expDate2str( date( "n/Y")); ?> Which when I run it (in PHP 4 though) prints out this: Invalid, blowing up date In: 1/2009 Out: 2009-01-01 Invalid, blowing up date In: 8/2006 Out: 2006-08-01 For the heck of it, here's a more production-oriented version of the function/test script: <?php function expDate2str($date) { $datelen = strlen($date); if ( ($datelen == 6 || $datelen == 7) && substr_count($date, "/") == 1 ) return date("Y-m-d", strtotime(str_replace("/", "/01/", strlen($date) == 6 ? "0$date" : $date))); else return date("Y-m-d", $date); } echo expDate2str('1/2009'); echo "<br/>"; echo expDate2str( date( "n/Y")); ?> Regards, Adam Zey. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php