Location: PHPKode > projects > Kwalbum > kwalbum/include/funcChangeDate.php
<?php
// 2.0
function ChangeDate($id, $newDate, $echo = true)
{
	global $DB, $P, $errorMsg;

	$newDate = trim($newDate);
	$new = explode('-', $newDate);
	$newYear = (int)$new[0];
	$newMonth = (int)$new[1];
	$newDay = (int)$new[2];
	$newNewDate = "$newYear-$newMonth-$newDay";

	if ('0-0-0' == $newNewDate and '' != $newDate and '0000-00-00' != $newDate)
		$badDate = true;
	else
	{
		$badDate = false;
		$newDate = $newNewDate;
	}

	$query = 'SELECT ItemDate, ItemTime, ItemOrderBy, ItemUserIdFk FROM ' . ITEM_TABLE .
	" WHERE ItemId='$id'" .
	" LIMIT 1";
	$result = $DB->Query($query);
	if (0 == $result->num_rows)
	{
		if (TEST_MODE)
			echo '<p class="error">Item ID ('.$id.') was not found in the DB.</p>';
		else if ($echo)
			echo $errorMsg;
		return;
	}
	$row = $result->fetch_array();
	if (!USER_CAN_ADD or (USER_ID != $row[3] and !USER_IS_ADMIN))
	{
		if (TEST_MODE)
			echo '<p class="error">Invalid Permissions</p>';
		else if ($echo)
			echo $errorMsg;
		return;
	}

	$oldDate = $row[0];
	$old = explode('-', $oldDate);
	$oldMonth = $old[1];
	$oldYear = $old[0];
	$oldTime = $row[1];
	$oldOrderBy = $row[2];
	if ($badDate)
		$newDate = $oldDate;

	if ($oldDate != $newDate)
	{
		if ($oldOrderBy == "$oldDate $oldTime")
			$DB->UpdateSingle(ITEM_TABLE, 'ItemOrderBy', "'$newDate $oldTime'", "ItemId='$id'");
		$DB->UpdateSingle(ITEM_TABLE, 'ItemDate', "'$newDate'", "ItemId='$id'");

		$query = 'SELECT DateCount FROM ' . DATE_TABLE .
		" WHERE DateYear='$newYear' AND DateMonth='$newMonth'" .
		" LIMIT 1";
		$result = $DB->Query($query);
		if (0 == $result->num_rows)
		{
			$insertData = array (
				'DateYear' => $newYear,
				'DateMonth' => $newMonth
			);
			if (false === $DB->Insert(DATE_TABLE, $insertData))
			{
				if (TEST_MODE)
					echo '<p class="error">Error inserting into date table.</p>';
				else if ($echo)
					echo $errorMsg;
				return;
			}
		}
		$DB->UpdateSingle(DATE_TABLE, 'DateCount', 'DateCount-1', "DateYear='$oldYear' AND DateMonth='$oldMonth'");
		$DB->UpdateSingle(DATE_TABLE, 'DateCount', 'DateCount+1', "DateYear='$newYear' AND DateMonth='$newMonth'");
	}
	if ($echo)
		echo $newDate;
}
?>
Return current item: Kwalbum