Jump to content

Xml Listing Last 3 Months -> February Not Getting Listed


Greywacke

Recommended Posts

hi, i need to output the last 3 months including the current month, and it has worked till march.here is the code that does this:

// get the current time$now = time();// function to list the monthsfunction last3months($name) {	$now = $GLOBALS["now"];	//echo "\$m = ".$GLOBALS["m"].";\n";	//echo "date(\"Y-m\", \$now) = ".date("Y-m", $now).";\n";	echo "	<statmonths name=\"".$name."\">\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 "	</statmonths>\n";}

how could i improve on this to get months < 30 days listed after that month? basically i need to print the monthname and year (format "F Y") as the text and a numeric representation in the format "Y-m" as value attributes.could someone please assist? dates and times are not exactly my forte...

Link to comment
Share on other sites

You could either try to explicitly detect februrary, or (what I'd instead try to do) use the DateTime class. Set it to the current date (i.e. just construct it... it uses the current date and time by default), sub two months, then print each result, after which add a month.

Link to comment
Share on other sites

well i cannot use the date_add or date_sub functions, as the production environment uses PHP 5.2.9 which does not have these functions yet... its supposed to subtract a month from the time to go one month back. the listing is in reverse order, and meant to be that way unfortunately. as i said i am not that confident when working with date/time objects.this is however of secondary importance at the moment, repairing that function to write the months in reverse order from the current month.

Link to comment
Share on other sites

The low tech solution would be a combination of getdate and mktime. getdate will return all of the information about a timestamp (month, day, hour, etc) in an array, and mktime will make a timestamp from a given date and time. So if you're working with timestamps, you can make a new timestamp for the previous month by using getdate to get the month and year of the current timestamp, then using mktime to make a new one with the same year and month - 1. mktime is pretty flexible, if you give it a date that doesn't exist (like 13/1/2009) it will convert it to what you would expect (1/1/2010)

Link to comment
Share on other sites

ah thanks, will look into this :)

Link to comment
Share on other sites

okay, the requirements have changed slightly, and i've started implementing as suggested by justsomeguy - and here is the code:

require_once('timezones/class-timezone-conversion.php');   /** Include class */function wdint($weekday) {	switch ($weekday) {		case "Saturday":			return 6;			break;		case "Friday":			return 5;			break;		case "Thursday":			return 4;			break;		case "Wednesday":			return 3;			break;		case "Tuesday":			return 2;			break;		case "Monday":			return 1;			break;		case "Sunday":			return 0;			break;	}}function convert_tz($timestamp, $timezone) {	/** convert local datetime to SAST (South African Standard Time) */	$tz = new TimezoneConversion();		/** Create TimezoneConversion Object */	$tz->setProperty('DateTime', $timestamp);	/** Set local 'DateTime' to convert */	$tz->setProperty('Timezone', $timezone);	/** Set Timezone Convert To */	return $tz->convertDateTime();			/** Get SAST Timestamp */}function get_timeframe($timeframe, $timezone) {	$now =		getdate(			strtotime(				convert_tz(					date("Y-m-d H:i:s", time()),					$timezone				)			)		);	switch ($timeframe) {		case "today":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"],						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "yesterday":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				);			break;		case "week to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - wdint($now["weekday"]),						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last week":						break;		case "month to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last month":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"] - 1,						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"] - 1,						[b]$now["mday"],[/b]						$now["year"]					)				);			break;	}}

the get_timeframe function, returns in the format "2010-03-09 00:00:00 to 2010-03-09 23:59:59" with "Today" as the timeframe passed. what's buggering my mind though, is how to implement "Last Week" or finish implementing "Last Month".can anybody please help me here?the result i am to get is the following:

<select name="menu_statsel" id="menu_statsel">	<option value="2010-03-09 00:00:00 to 2010-03-09 23:59:59" selected="selected">Today</option>	<option value="2010-03-08 00:00:00 to 2010-03-08 23:59:59">Yesterday</option>	<option value="2010-03-08 00:00:00 to 2010-03-09 23:59:59">Week to Date</option>	<option value="2010-03-01 00:00:00 to 2010-03-07 23:59:59">Last Week</option>	<option value="2010-03-01 00:00:00 to 2010-03-09 23:59:59">Month to Date</option>	<option value="2010-02-01 00:00:00 to 2010-02-28 23:59:59">Last Month</option></select>

Link to comment
Share on other sites

For the end of last month you can subtract 1 from the start of this month

					mktime(						0,						0,						0,						$now["mon"],						1,						$now["year"]					) - 1

For last week, are you just trying to get the previous 7 days and that's it?

Link to comment
Share on other sites

yeah last month was kinda,well - duh. especially now that now that u mentioned it :)lol i really need airconditioning in this office 0o

Link to comment
Share on other sites

hi again,here is the updated code... i can think alot better during the evening when its cooler in the office :)

<?phprequire_once('timezones/class-timezone-conversion.php');   /** Include class */function wdint($weekday) {	switch ($weekday) {		case "Sunday":			return 6;			break;		case "Saturday":			return 5;			break;		case "Friday":			return 4;			break;		case "Thursday":			return 3;			break;		case "Wednesday":			return 2;			break;		case "Tuesday":			return 1;			break;		case "Monday":			return 0;			break;	}}function convert_tz($timestamp, $timezone) {	/** convert local datetime to SAST (South African Standard Time) */	$tz = new TimezoneConversion();			/** Create TimezoneConversion Object */	$tz->setProperty('DateTime', $timestamp);	/** Set local 'DateTime' to convert */	$tz->setProperty('Timezone', $timezone);	/** Set Timezone Convert To */	return $tz->convertDateTime();			/** Get SAST Timestamp */}function get_timeframe($timeframe, $timezone) {	$now =		getdate(			strtotime(				convert_tz(					date("Y-m-d H:i:s", time()),					$timezone				)			)		);	switch ($timeframe) {		case "today":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"],						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "yesterday":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				);			break;		case "week to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - wdint($now["weekday"]),						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last week":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - wdint($now["weekday"]) - 7,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"] - wdint($now["weekday"]),						$now["year"]					)				);			break;		case "month to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last month":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"] - 1,						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						-1,						$now["year"]					)				);			break;	}}

the convert_tz function uses the TimeZone Conversion PHP class by Utsav Handa (http://www.phpclasses.org/browse/package/5274.html)just wish to get opinions on this current code, before it will be marked as resolved, but i'll probably get this done in the sandbox version of the site. :)

Link to comment
Share on other sites

well well, looks like the issue has been resolved now :) both last week and last month work ^^here is the updated code... 0 is the last day of the previous month with mktime here, though...

require_once('timezones/class-timezone-conversion.php');   /** Include timezone conversion class */function wdint($weekday) {	switch ($weekday) {		case "Sunday":			return 6;			break;		case "Saturday":			return 5;			break;		case "Friday":			return 4;			break;		case "Thursday":			return 3;			break;		case "Wednesday":			return 2;			break;		case "Tuesday":			return 1;			break;		case "Monday":			return 0;			break;	}}function convert_tz($timestamp, $timezone) {	/** convert local datetime to SAST (South African Standard Time) */	$tz = new TimezoneConversion();			/** Create TimezoneConversion Object */	$tz->setProperty('DateTime', $timestamp);	/** Set local 'DateTime' to convert */	$tz->setProperty('Timezone', $timezone);	/** Set Timezone Convert To */	return $tz->convertDateTime();			/** Get SAST Timestamp */}function get_timeframe($timeframe, $timezone) {	$now =		getdate(			strtotime(				convert_tz(					date("Y-m-d H:i:s", time()),					$timezone				)			)		);	switch ($timeframe) {		case "today":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"],						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "yesterday":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"] - 1,						$now["year"]					)				);			break;		case "week to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - wdint($now["weekday"]),						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last week":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						$now["mday"] - wdint($now["weekday"]) - 7,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"] - wdint($now["weekday"]) - 1,						$now["year"]					)				);			break;		case "month to date":			return				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"],						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						$now["mday"],						$now["year"]					)				);			break;		case "last month":			return 				date("Y-m-d 00:00:00",					mktime(						0,						0,						0,						$now["mon"] - 1,						1,						$now["year"]					)				).				" to ".				date("Y-m-d 23:59:59",					mktime(						23,						59,						59,						$now["mon"],						0,						$now["year"]					)				);			break;	}}

Link to comment
Share on other sites

ps: i noticed that there were a few unnessessary instances of date and strtotime... therefore, i made it only with the getdate function now, which expects a numerical timestamp. :)the getdate function was necessary to give an array, so the dates can be set accordingly this way. :)thanks for your help, justsomeguy - much appreciated :)

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...