Jump to content

dateadd function for adding days to current date.


Greywacke

Recommended Posts

hi there,currently i am testing a new function to automatically calculate the date a product can be expected.the code is as follows:

<?phpinclude('includes/statistical/timezones/class-timezone-conversion.php');   /** Include timezone conversion class */// test timezone conversion of current date with dateadd.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 dateadd($interval) { 	$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", time()),"SAST")));	return date("Y-m-d H:i:s",mktime(23,59,59,$now["mon"],$now["day"] + $interval,$now["year"]));}function getexpected($fitment) {	switch ($fitment) {		case "ASAP":			return dateadd(7);			break;		case "within_2_weeks":			return dateadd(14);			break;		case "within_month":			return dateadd(30);			break;		case "within_2_months":			return dateadd(60);			break;		case "within_3_months":			return dateadd(90);			break;		case "not_within_3_months":			return dateadd(120);			break;		default:			return "0000-00-00 23:59:59";	}}echo dateadd(7);echo "<br />\n";echo getexpected("within_2_weeks");?>

and it returns as follows:

2010-10-07 23:59:592010-10-14 23:59:59

this is almost right, except that it doest add 7 or 14 days to todays date which is the 25th. i have absolutely no idea why this would be. somebody please help!

Link to comment
Share on other sites

ah it should have added days to the current date, and the value i was looking for is not day. it is mday.this function works adding the amount of days passed as parameter to the current date and returning one minute to midnight SAST on that date. :)

function dateadd($interval) { 	$now = getdate(strtotime(convert_tz(date("Y-m-d H:i:s", time()),"SAST")));	return date("Y-m-d H:i:s",mktime(23,59,59,$now["mon"],$now["mday"] + $interval,$now["year"]));}

Link to comment
Share on other sites

it gets it now with the dateadd function, but for some reason if i do

$expected				= getexpected($attribsarr["fitment"]);

then it does not insert it into the database when i call this update, about 15 lines down:

$sql1 = "INSERT INTO 6_serviceleads (text_Consumer, text_LeadSubject, text_LeadAttributes, timestamp_ExpectedBy, text_LeadMessage, bigint_ServiceID, bigint_SupplierID, bigint_RegionID) VALUES (\"".$consumerfullname.";".$consumeremail.";".$consumercell.";".$city_town."\",\"".$append."\",\"".$attribs."\",\"".$expected."\",\"".$message."\",".$service.",".$recipient[6].",".$region.");";$result1 = mysql_query_errors($sql1, $conn , __FILE__ , __LINE__ , false);

it does not use the variable $expected anywhere else on the page.

Link to comment
Share on other sites

well the problem is somewhere between where the function is called and where the variable is used.i have marked these lines as bold and red.

[color="#FF0000"][b]$expected = getexpected($attribsarr["fitment"]);[/b][/color]$tsql = "SELECT * FROM 7_blacklist WHERE `text_E-mailAddress` = \"".$consumeremail."\";";$result = mysql_query_errors($tsql, $conn , __FILE__ , __LINE__ , false);if (mysql_errno()> 0) {	$sql .= $tsql;	$sql .= strtoupper($err)."\n";}if ($result) {	if (mysql_num_rows($result) > 0) {		if ($row = mysql_fetch_array($result)) {			echo mysql_real_escape_string("Your e-mail address has been "."blacklisted\n"."for ".$row["text_ListedReason"].".\n\nPlease mail info@ferrety.co.za for more ". "information.");		}	} else {		getsuppliers($service,0);		dropattribs(0);		$x = supplierswanted(0, $service, $region);		while (count($mailadds) > $x) { array_pop ($mailadds); }		//echo count($mailadds);// loop through available suppliers and their recipients		foreach ($mailadds as $recipient) {// calculate service cost for the supplier			$servicecost = costbycategory($service,$attribsarr,$recipient[9]);// check duplicate days			$append = duplicatedays($consumeremail,$service,$recipient[6]);			[color="#FF0000"][b]$sql1 = "INSERT INTO 6_serviceleads ("."text_Consumer, text_LeadSubject, text_LeadAttributes, timestamp_ExpectedBy, "."text_LeadMessage, bigint_ServiceID, bigint_SupplierID, bigint_RegionID) VALUES "."(\"".$consumerfullname.";".$consumeremail.";".$consumercell.";".$city_town."\",\"".$append."\",\"".$attribs."\",\"".[u]$expected[/u]."\",\"".$message."\",".$service.",".$recipient[6].",".$region.");";[/b][/color]

the attributes get inserted though, and are available on the page - yet the variable $expected gets dropped for some reason.

Link to comment
Share on other sites

ok i wrote the variable down after getting it, and its all perfect. only problem is the $expected variable is dropped in the 21 lines before its used 0o.

Link to comment
Share on other sites

ok pasted the following line between every line and the next, in a test:

echo $expected;

and got 10 dates. as the flow of code goes, i should have received 11 - this means that between the following two lines it is dropping the variabl.

			$servicecost = costbycategory($service,$attribsarr,$recipient[9]);				$append = duplicatedays($consumeremail,$service,$recipient[6]);

i might need to speak to vodahost about increasing memory availability for the scripts on this site - i certainly hope that will be the end of this issue.

Link to comment
Share on other sites

It sounds like one of those two functions is modifying the value. Is the value assigned by reference?This isn't a memory issue, memory issues will cause errors instead of allowing the program to continue with bad memory. If a script runs out of memory it terminates immediately, it doesn't corrupt things and just continue.

Link to comment
Share on other sites

then what could the problem that i am facing be? the sql query adds leads to the database, just with blank expected by dates... :)even renaming the $expected variable to $exby (definately not used elsewhere! didn't do a thing. yet if i print it before i do the sql query, i get it... 0o

Link to comment
Share on other sites

ohhhhh... i was following the flow of code wrongly. i missed an if statement, and only if that statement was true was it updating. else it also updates - but i didnt include the timestamp_ExpectedBy field yet in that update. i kept testing on the else... 0othis issue is now resolved ^^ going to write a php page to get the attributes and update the existing records which have the default value of 0000-00-00 23:59:59.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...