Jump to content

Trying To List Months Since A Fixed Date


Greywacke

Recommended Posts

okay, i'm trying to generate dates so the user can select via ajax which transactions to report. all the code works, except the date_add or date_sub functions - the first variable does not want to be accepted, as it is an integer.i have two lists to generate, to and from dates (months really). they need to be exported to xml as the following code suggests:

function getfromdates() {	$f = strtotime(date($GLOBALS["f"]));	//while ($f < strtotime(date($GLOBALS["t"]))) {		echo "	<fromdate>\n";		echo "		<text>".date("F Y",$f)."</text>";		echo "		<value>".date("Y-m-d H:i:s",$f)."</value>";		if ($f == $GLOBALS["f"]) echo "		<selected>selected</selected>\n";		echo "	</fromdate>\n";		$f = date_add($f,new DateInterval("P1M"));	//}}function gettodates() {	$t = strtotime(date($GLOBALS["t"]));	//while ($t > strtotime(date($GLOBALS["f"]))) {		echo "	<todate>\n";		echo "		<text>".date("F Y",$t)."</text>";		echo "		<value>".date("Y-m-d H:i:s",$t)."</value>";		if ($t == $GLOBALS["t"]) echo "		<selected>selected</selected>\n";		echo "	</todate>\n";		$t = date_sub($t,new DateInterval("P1M"));	//}}

the selected (default for the page) to and from dates are in $t and $f variables as following code suggests, at the top of the xml generating document.

$f = $_GET["f"];if (!strtotime(date($f))) {	$f = date("2010-01-01 00:00:00");}$t = $_GET["t"];if (!strtotime(date($t))) {	$t = date("Y-m-t 23:59:59");}

now i am battling to send the dates in the loops as DateTime values not Integer or Boolean, i've tried mktime and strtotime as suggested by the pages retrieved from google, unfortunately the documentation on these two functions (date_add and date_sub), is very limited. someone please help!

Link to comment
Share on other sites

well i have updated the functions, and here they are. no more errors, this issue has been resolved :)

function getfromdates() {	$f = strtotime(date($GLOBALS["f"]));	while ($f < strtotime(date($GLOBALS["t"]))) {		echo "	<fromdate>\n";		echo "		<text>".date("F Y",$f)."</text>\n";		echo "		<value>".date("Y-m-d H:i:s",$f)."</value>\n";		if (date("Y-m-d H:i:s",$f) == $GLOBALS["f"]) echo "		<selected>selected</selected>\n";		echo "	</fromdate>\n";		$dt = date_add(new DateTime(date("Y-m-d H:i:s",$f)),new DateInterval("P1M"));		$dtn = $dt->format("Y-m-d H:i:s");		$f = strtotime(date($dtn));	}}function gettodates() {	$t = strtotime(date($GLOBALS["t"]));	while ($t > strtotime(date($GLOBALS["f"]))) {		echo "	<todate>\n";		echo "		<text>".date("F Y",$t)."</text>\n";		echo "		<value>".date("Y-m-d H:i:s",$t)."</value>\n";		if (date("Y-m-d H:i:s",$t) == $GLOBALS["t"]) echo "		<selected>selected</selected>\n";		echo "	</todate>\n";		$dt = date_sub(new DateTime(date("Y-m-d H:i:s",$t)),new DateInterval("P1M"));		$dtn = $dt->format("Y-m-d H:i:s");		$t = strtotime(date($dtn));	}}

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...