Location: PHPKode > projects > QUICKLYWEB > quicklyweb/admin/datetest.php

$db = getDBConnection(); 
	#Get the number of days next month.
	#Note: we add two months to this one and use 0 for the days
	#to return the number of days in the month.
$nextmonth = mktime (0,0,0,date("m")+1,date("d"),date("Y"));

	#Get the number of days this month.
	#Note: we add one month to this one and use 0 for the days
	#to return the number of days in the month.
$thismonth = mktime (0,0,0,date("m")+1,0,date("Y"));

	#Get the number of days last month. This time we dont add
	#anything to the month and we get the number of days for last
$lastmonth = mktime (0,0,0,date("m"),0,date("Y"));

if (date("d", $thismonth) >= date("d", $lastmonth)){
#We dont need to check any further because all months have 28 days :)
echo "dont need more checks<br>";
$onemonthago = mktime (0,0,0,date("m")-1,date("d"),date("Y"));
$now = date("Y-m-d", $onemonthago);
$thisnow = date("Y-m-d");
$nextnow = date("Y-m-d", $nextmonth);

	# Select all the accounts which havnt been billed yet or have a last billed date of today
	# last month month and have not been marked as closed.

$sql = "SELECT A.AccountID, A.CustomerID, A.Username, A.DateOpened, A.LastDateBilled, PackageGroup.Price, PackageGroup.Tax, PackageGroup.Description";
$sql .= " FROM Accounts AS A RIGHT JOIN PackageGroup ON PackageGroup.PackageGroupID=A.PackageGroupID";
$sql .= " WHERE (A.LastDateBilled='2003-02-02') OR (A.LastDateBilled='0000-00-00')";
$sql .= " AND (A.Status = 'Open') AND (DateClosed = '0000-00-00') order by Username";

$bill_result = DBquery($sql, $db);

	# Step through the results.
while($bill_row = DBfetch_array($bill_result)){
$sqlbill = NULL;

	# Insert the new Invoice into the Invoices table.
$sqlbill = "INSERT INTO Invoices VALUES('','".$bill_row["CustomerID"]."','$thisnow','Open','No')";
//echo $sqlbill;
DBquery($sqlbill, $db);

	# Get the insert_id for the Invoice to be used on the InvoiceItems Table.
$insertid = mysql_insert_id();

	# Calculate the Tax for the new Invoice.
$taxtotal = ($bill_row["Tax"]/100) * $bill_row["Price"];

	# Insert the InvoiceItems information along with the dates.
$sqlbillitem = "INSERT INTO InvoiceItems VALUES('','$insertid','".$bill_row["Description"];
$sqlbillitem .= ", $thisnow to $nextnow - Username: ".$bill_row["Username"]."','".$bill_row["Price"]."','$taxtotal')";
//echo $sqlbillitem;
DBquery($sqlbillitem, $db);

	# Update the Accounts table. Set the LastDateBilled field to todays date.
$accountsql = "UPDATE Accounts SET LastDateBilled='$thisnow' WHERE AccountID='".$bill_row["AccountID"]."'";
//echo $accountsql;
DBquery($accountsql, $db);


} else {
#Now we pull out what the date range is for last month
echo "date larger than the shortest month";

echo date("d", $thismonth);
echo date("d", $lastmonth);
echo date("d", $nextmonth);
Return current item: QUICKLYWEB