Re: date(n/Y) strtotime

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mark Steudel wrote:
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

I usually just munge the string and toss it into strtotime.

For credit card expiration dates, I'd just do this (Not going to the lengths of your solution, just the conversion part):

$timestamp = strtotime(str_replace("/", "/01/", $date));

That will turn 9/2006 into 9/01/2006 which is a GNU compatible date that strtotime should be able to understand. It assumes that the input date is in a certain format, but it's simpler than your solution of exploding, then using date and mktime.

One caveat is that I'm uncertain if you need to have a trailing zero for the month. The GNU page is somewhat unclear as it has conflicting info. You may simply want to do these two lines instead:

if ( strlen($date) == 6 ) $date = "0" . $date;
$timestamp = strtotime(str_replace("/", "/01/", $date));

Which will add the preceding zero to a date in the 9/2006 format if required. Those two lines would get you 09/01/2006

Here's an even simpler example, which uses a ternary operator to do it in one line:

$timestamp = strtotime(str_replace("/", "/01/", strlen($date) == 6 ? "0" . $date : $date));

That will also result in 09/01/2006. Note that where we're passing str_replace the source string to work on, we have a ternary (think of it as an inline if statement) that checks the length, and either returns $date, or "0" . $date.

Here's a slower (execution-wise) but easier to read version:

$timestamp = strtotime(str_replace("/", "/01/", strlen($date) == 6 ? "0$date" : $date));

Where I just did the concatenation inside a string, which I find easier to read.

All these examples assume that your date is in a fixed format, namely mm/yyyy. However, the last three should properly handle 09/2006, because they won't add a second zero to that. So that should work either way.

Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux