Jump to content

date/time issues (mktime, getdate, strtotime and date) - a little help please?


Greywacke
 Share

Recommended Posts

hi all,it's me again with dates. i've written the following code to write the past 4 months including the current month in ascending order, as well as the past 4 months including the current month in descending order - to print out the dates in xml for population in two dropdowns via ajax.now, i can't seem to get this right:

// Include timezone conversion classrequire_once('../includes/statistical/timezones/class-timezone-conversion.php');// get current SAST timestamp as getdate array to use in mktime$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", time()),"SAST")));// get timestamp from start$fs = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,	$now["mon"]-3,1,$now["year"])));// get timestamp from limit$fl = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,	$now["mon"],1,$now["year"])));// get timestamp to start$ts = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,	$now["mon"],0,$now["year"])));// get timestamp to limit$tl = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,	$now["mon"]-3,0,$now["year"])));// timezone conversion functionfunction 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}// subtract monthsfunction monthsub($date, $month, $timezone) {	$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", $date),$timezone)));	return strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"]-		$month,1,$now["year"])));}// add monthsfunction monthadd($date, $month, $timezone) {	$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", $date),$timezone)));	return strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]+		$month,0,$now["year"])));}// get from dates functionfunction getfromdates() {	$f = strtotime(date($GLOBALS["fs"]));	while ($f <= $GLOBALS["fl"]) {		echo "	<fromdate>\n";		echo "		<text>1st ".date("F Y, H:i:s",$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";		$f = monthadd($f,1,"SAST");	}}// get to dates functionfunction gettodates() {	$t = strtotime(date($GLOBALS["fs"]));	while ($t >= $GLOBALS["tl"]) {		echo "	<todate>\n";		echo "		<text>".date("tS F Y, H:i:s",$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";		$t = monthsub($t,1,"SAST");	}}

i am using the timezone conversion class and am trying to implement the getdate object along with mktime.how would i be able to get this done? the to dates all need to be the last day of the month, at 23:59:59 and the from dates all need to be the first day of the month at 00:00:00.the server is in the US, so the current dates need to be converted to +2:00 GMT (SAST) to correctly select the values from the database, which has +2:00 GMT (SAST) as the session timezone.however, with the above code, i only get the following todate in the xml output:

<fromdate>	<text>1st January 1970, 02:00:00</text>	<value>1970-01-01 02:00:00</value></fromdate>

that is all output i get from the execution of both of these functions, how does this work?

getfromdates();gettodates();

the globals ts and fs contain the from and to starting dates.the globals tl and fl contain the from and to ending dates.the globals t and f contain the selected dropdown values on the client - passed via the querystring.the from and to dates contain the current months first and last second respectively.please help anybody who can see what i do not! battling to see the forest for the trees again...

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay the code has been updated - i realised converting by timezone was not needed.

$now = getdate(strtotime(date("Y-m-d H:i:s", time())));$fs = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"]-3,	1,$now["year"])));$fl = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"],	1,$now["year"])));$ts = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"],	0,$now["year"])));$tl = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-3,	0,$now["year"])));function monthsub($date, $month, $timezone) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"]-		$month,1,$now["year"])));}function monthadd($date, $month, $timezone) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]+		$month,0,$now["year"])));}function getfromdates() {	$f = $GLOBALS["fs"];	while ($f <= $GLOBALS["fl"]) {		echo "	<fromdate>\n";		echo "		<text>1st ".date("F Y, H:i:s",$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";		$f = monthadd($f,1,"SAST");	}}function gettodates() {	$t = $GLOBALS["ts"];	while ($t >= $GLOBALS["tl"]) {		echo "	<todate>\n";		echo "		<text>".date("tS F Y, H:i:s",$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";		$t = monthsub($t,1,"SAST");	}}

but unfortunately it loops ad infinit... why does it never bring t = ts to tl and f = fs to fl? it is intended to print the previous 3 months from and to timestamps as well as the current month.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay, i noticed the monthadd was subtracting months and the monthsub was adding months, also the times and days of the month were swopped. here are the new functions:

$now = getdate(strtotime(date("Y-m-d H:i:s", time())));$fs = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"]-3,	1,$now["year"])));$fl = strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"],	1,$now["year"])));$ts = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"],	0,$now["year"])));$tl = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-3,	0,$now["year"])));function monthadd($date, $month, $timezone) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-1 00:00:00",mktime(0,0,0,$now["mon"]+		$month,1,$now["year"])));}function monthsub($date, $month, $timezone) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-		$month,0,$now["year"])));}function getfromdates() {	$f = $GLOBALS["fs"];	while ($f <= $GLOBALS["fl"]) {		echo "	<fromdate>\n";		echo "		<text>1st ".date("F Y, H:i:s",$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";		$f = monthadd($f,1);	}}function gettodates() {	$t = $GLOBALS["ts"];	while ($t >= $GLOBALS["tl"]) {		echo "	<todate>\n";		echo "		<text>".date("tS F Y, H:i:s",$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";		$t = monthsub($t,1);	}}

but unfortunately, these are the months returned in xml, after calling these two functions:

<fromdate>	<text>1st January 2010, 00:00:00</text>	<value>2010-01-01 00:00:00</value></fromdate><fromdate>	<text>1st February 2010, 00:00:00</text>	<value>2010-02-01 00:00:00</value></fromdate><fromdate>	<text>1st March 2010, 00:00:00</text>	<value>2010-03-01 00:00:00</value></fromdate><fromdate>	<text>1st April 2010, 00:00:00</text>	<value>2010-04-01 00:00:00</value>	<selected>selected</selected></fromdate><todate>	<text>31st March 2010, 23:59:59</text>	<value>2010-03-31 23:59:59</value></todate><todate>	<text>31st January 2010, 23:59:59</text>	<value>2010-01-31 23:59:59</value></todate>

only the from dates are correct, why would the to dates not be correct?any assistance in sorting these issues out, would be immensely appreciated - it would seem my clarity of intention has vapourised as result of the heat.

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

okay, i stand corrected.only the current timestamp should be of SAST, so that code needs to be updated as follows:require_once('../includes/statistical/timezones/class-timezone-conversion.php'); // Include timezone conversion class

// Include timezone conversion classrequire_once('../includes/statistical/timezones/class-timezone-conversion.php');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}$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", time()),"SAST")));

i am still sitting with the problem of parsing the to dates however, from the current month to 4 months ago. for some reason the following code only allows months with 31 days to be parsed 0o

function monthsub($date, $month) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-		$month,0,$now["year"])));}

Edited by Pierre 'Greywacke' du Toit
Link to comment
Share on other sites

success!!! :)using 0 as the getdate day parameter gets the last day of the previous month - so i had to add 1 to get the last month of the current month.here is the adjusted code:

$ts = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]+	1,0,$now["year"])));$tl = strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-	2,0,$now["year"])));function monthsub($date, $month) {	$now = getdate(strtotime(date("Y-m-d H:i:s", $date)));	return strtotime(date("Y-m-t 23:59:59",mktime(23,59,59,$now["mon"]-$month+1,0,$now["year"])));}

this issue is now resolved :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...