Jump to content

Date Validation Problem


Greywacke

Recommended Posts

hi... i'm battling to get and validate a date in format YYYY-MM-DD with the following code. i am trying to write an xml block of the past 3 months, $m signifies the selected value on the client. this is used with an ajax script.i've googled for this problem, asked friends on irc, but they are stumped as well. all i managed to come up with is the !strtotime(date($value)) validation of querystring data.i've also tried validating with a time as shown below (to validate as a unix style timestamp).added checking for blank values.testing, the value is sent through the querystring.

$now = time();$m = $_GET["m"];if ($m == "" || !strtotime(date($m."-01 00:00:00"))) {	$m = date("Y-m", $now);}$sql = "";function last3months() {	$now = $GLOBALS["now"];	echo "\$m = ".$m.";\n";	echo "date(\"Y-m\", \$now) = ".date("Y-m", $now).";\n";	for($i = 0; $i < 3; $i++){		echo "	<months>\n";	 	echo "		<month text=\"".date("F Y", $now)."\" value=\"".date("Y-m", $now)."\"".(($m == date("Y-m", $now))?" selected=\"selected\"":"")." />\n";		echo "	</months>\n";		$now -= (intval(date("t",$now)) * 24 * 60 * 60);	}}

here is the xml i get from this code:

$m =;date("Y-m", $now) = 2010-01;	<months>		<month text="January 2010" value="2010-01" />	</months>	<months>		<month text="December 2009" value="2009-12" />	</months>	<months>		<month text="November 2009" value="2009-11" />	</months>	<sql></sql>

Link to comment
Share on other sites

hahaha stupid me :) forgot to reference the toplevel $m with $GLOBALS from inside a function :)

$now = time();$m = $_GET["m"];if ($m == "" || !strtotime(date($m."-01 00:00:00"))) {	$m = date("Y-m", $now);}$sql = "";function last3months() {	$now = $GLOBALS["now"];	//echo "\$m = ".$GLOBALS["m"].";\n";	//echo "date(\"Y-m\", \$now) = ".date("Y-m", $now).";\n";	echo "	<months>\n";	for($i = 0; $i < 3; $i++){		echo "		<month text=\"".date("F Y", $now)."\" value=\"".date("Y-m", $now)."\"".(($GLOBALS["m"] == date("Y-m", $now))?" selected=\"selected\"":"")." />\n";		$now -= (intval(date("t",$now)) * 24 * 60 * 60);	}	echo "	</months>\n";}

and here is the wanted xml excerpt :)

	<months>		<month text="January 2010" value="2010-01" selected="selected" />		<month text="December 2009" value="2009-12" />		<month text="November 2009" value="2009-11" />	</months>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...