Some SQL

philaniH

Well-Known Member
Joined
Jun 25, 2013
Messages
471
Reaction score
7
Location
Newcastle
Hello Guys

I need some help with an update statement

my table

donationID
paymentDate
amount
reference
user_ID (fk)

i need to take business days from curdate() and paymentDate and reverse paymentDate to a date that will include the business days. (already have a php function to calculate the businessdays and also minus holidays)

Can you kill this one?
 
and reverse paymentDate to a date that will include the business days. (already have a php function to calculate the businessdays and also minus holidays)

Huh?

Also what DBMS?
 
The way programming forums normally work is you show us your attempt and then we help you along.

You also need to be more descriptive of the tables and the problem/question.
 
I think I get where you are going with this.

your Payment days are just dates, any day of the year.
your Business days are only 5 days of the week or whatever?


if you want to change your payment date to the closest business day you are going to have to use PHP to do this.

loop over your records, find the closes business day to the payment date, update the payment date, move to the next record.


yes there is a way you can do that in SQL, but then you have to create a table with all your business days, write a cursor and and and.
creating a simple PHP page will just be easier.
 
I'm assuming donation ID is a unique key.

SELECT * FROM myTable
for loop through db results(...)
{
if(!isBusinessDay(paymentDate))
{
updatePaymentDate(donationID,getNearestBusinessDay(paymentDate));
}
}

function updatePaymentDate(donationID, businessDay)
{
UPDATE myTable
set paymentDate = businessDay
where myTable.donationID = donationID
}
 
This sounds easy, can you show your attempts so far?

Update table
set paymentdate = phpfuntion(paymentdate)
where userid = 'userid'

???
 
I need this to reverse the whole date
ie
businessDays = 4

therefore date minus 4 Businessdays

say today is 04 Nov, 4 business days before today start at 31st oct.

I need the date to be set as 2015-10-31
 
Also - by holiday's do you mean public holidays?

You will most likely need to store these separately in a table and update them on a yearly basis.
 
look doing a dateadd -4 is the easy way out. but you need to know if any of the dates between the original and the new date are in the list of business days.

just write the PHP page to do it becasue trying to do this in just mySQL will take you much longer and will not be 100% accurate
 
Good practice would be to have a date table/dimension with indicator for weekend/public holiday/business day. Join, windows function or subquery, boom done.
 
Good practice would be to have a date table/dimension with indicator for weekend/public holiday/business day. Join, windows function or subquery, boom done.

If you can keep this up-to-date then this is the way to go.
 
FYI Solution

PHP:
<?php
define('DB_SERVER', "localhost");
define('DB_USER', "certified_eh");
define('DB_PASSWORD', "why?");
define('DB_DATABASE', "lindelan_101");
define('DB_DRIVER', "mysql");

class BusinessDaysCalculator {

    const MONDAY    = 1;
    const TUESDAY   = 2;
    const WEDNESDAY = 3;
    const THURSDAY  = 4;
    const FRIDAY    = 5;
    const SATURDAY  = 6;
    const SUNDAY    = 7;

    /**
     * @param DateTime   $startDate       Date to start calculations from
     * @param DateTime[] $holidays        Array of holidays, holidays are no conisdered business days.
     * @param int[]      $nonBusinessDays Array of days of the week which are not business days (Sat & Sun).
     */
    public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
        $this->date = $startDate;
        $this->holidays = $holidays;
        $this->nonBusinessDays = $nonBusinessDays;
    }

    public function minusBusinessDays($howManyDays) {
        $i = 0;
        while ($i < $howManyDays) {
            $this->date->modify("-1 day");
            if ($this->isBusinessDay($this->date)) {
                $i++;
            }
        }
    }

    public function getDate() {
        return $this->date;
    }

    private function isBusinessDay(DateTime $date) {
        if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
            return false; //Date is a nonBusinessDay.
        }
        foreach ($this->holidays as $day) {
            if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
                return false; //Date is a holiday.
            }
        }
        return true; //Date is a business day.
    }

 public function getDays() {
        return $this->date;
    }
}

// Get Normal Days Difference, reverse with business days. Boom Client is happy...
try {
    $db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER, DB_PASSWORD);
    
	$stmt = $db->prepare("SELECT help_id,DATEDIFF(CURDATE(),STR_TO_DATE(approvaldate, '%Y-%m-%d')) AS NormalDays FROM help");
	$stmt->execute();
			foreach( $stmt as $row )
			{
				$days = $row['NormalDays'];
				
				//inst BusinessDaysCalculator class, send days, get date.
				$calculator = new BusinessDaysCalculator(
				new DateTime(), // Today
				[new DateTime("2015-09-25")], // Existing holiday within expect range
				[BusinessDaysCalculator::SUNDAY, BusinessDaysCalculator::SATURDAY]);
				
				// Reverse
				$calculator->minusBusinessDays($days);
				//New Date 
				$reversedDate =$calculator->getDays();
				$newDate = $reversedDate->format('Y-m-d');
				
				
					if ($days>0){
								$sql = "UPDATE `help`   
								SET `approvaldate` = :newDate
								WHERE `help_id` = :help_id";
								$statement = $db->prepare($sql);
								$statement->bindValue(":help_id", $row['help_id']);
								$statement->bindValue(":newDate", $newDate);
								$statement->execute();
								echo "One Victim Down.";
								}			
			}
			
    $db = null;
} catch(PDOException $e) {
    trigger_error('Your IQ is 122 for a reason, Fix this buddy:' . $e->getMessage(), E_USER_ERROR);
}
 
Top
Sign up to the MyBroadband newsletter
X