1

Language used:PHP

I have a form to enter
CNumber
DateFrom
DateTo

On clicking Submit, I want the functionality to break up the dates into financial years.
Eg- INPUT Through Above Form
CNumber:1
DateFrom:12/12/2013
DateTo:31/03/2014

Then the ( OUTPUT )values to be inserted in table should be
CNumber:1
DurationFrom:12/12/2011
DurationTo:31/03/2012

CNumber:1
DurationFrom:01/04/2012
DurationTo:31/03/2013

CNumber:1
DurationFrom:01/04/2013
DurationTo:31/03/2014

I hope the logic is clear.
I'm unable to code the above problem.

Thanks for your help..!!

Techno
  • 11
  • 4

2 Answers2

1

You can use mktime() function to reach your out put! for example there is a simple code but if you looking for it more you could write that you want :

list($df_day,$df_month,$df_year) = split('/',$durationFrom);
list($dt_day,$dt_month,$dt_year) = split('/',$durationTo);
$newDurationFrom = date("dd/mm/yyyy",mktime(0,0,0,$df_month,$df_day,($df_year+[YOUR VALUE HERE])));
$newDurationTo = date("dd/mm/yyyy",mktime(0,0,0,$dt_month,$dt_day,($dt_year-[YOUR VALUE HERE])));
Hamid
  • 1,700
  • 2
  • 13
  • 19
0

Assuming that $cNumber, $dateFrom, $dateTo are your PHP variables that are storing the CNumber, From date and To date respectively after the form is submitted.

$objDateFrom = new DateTime(str_replace('/', '-', $dateFrom));  // create DateTime object for from-date
$objDateTo = new DateTime(str_replace('/', '-', $dateTo));  // create DateTime object for to-date
$fromYear = $objDateFrom->format('Y');
$toYear = $objDateTo->format('Y');
$fromDate = $objDateFrom->format('Y-m-d');
$toDate = $objDateTo->format('Y-m-d');

$arrObjDates[] = new DateTime($fromDate);   // this array stores DateTime objects for from-date and to-date plus all intermediary dates; by default it always stores from-date as the first element
for ($i = $fromYear; $i <= $toYear; $i++) {
    $tmpDate = $i . '-03-31';
    if ($i == $fromYear) {  // the first iteration
        if ($fromDate > $tmpDate) { // if from-date is after the FY end date in the same year, skip further loop execution
            continue;
        }
    }
    if ($i == $toYear) {    // the last iteration
        if ($toDate > $tmpDate) {   // if to-date is after the FY end date in the same year, store the FY end date 'yyyy-03-31'
            $arrObjDates[] = new DateTime($tmpDate);
        }
        $arrObjDates[] = new DateTime($toDate);
    } else {
        $arrObjDates[] = new DateTime($tmpDate);
    }
}

$qry = '';
if (!empty($arrObjDates)) {
    $qry = 'INSERT INTO `tableA` (`c_number`, `date_from`, `date_to`) VALUES';
    for ($idx = 0; $idx < count($arrObjDates) - 1; $idx++) {
        if ($idx > 0) {
            $qryFromDate = $arrObjDates[$idx]->format('Y') . '-04-01';
        } else {
            $qryFromDate = $arrObjDates[$idx]->format('Y-m-d');
        }
        $qry .= '(' . $cNumber . ', \'' . $qryFromDate . '\', \'' . $arrObjDates[$idx+1]->format('Y-m-d') . '\'),';
    }
}
$qry = trim($qry, ',');

It took me a while to code this but at the end I hope it should work. I didn't try to simplify the code but I'm sure there are ways to do so.

Abhay
  • 6,545
  • 2
  • 22
  • 17
  • Brilliant effort on your part Abhay!! But I'm unable to implement it in a PHP file. Can you upload your above file and post the URL please? – Techno Jun 30 '11 at 20:00
  • Thanks @Techno. My code above is the only code that the PHP script has; there's nothing more in it. What problem are you having implementing it? – Abhay Jul 01 '11 at 04:15
  • There is some problem while insertion, it is displaying me the query instead. Since you have already implemented the code, can you please share that WORKING file? – Techno Jul 01 '11 at 04:59
  • INSERT INTO tableA (c_number, date_from,date_to) VALUES(1, '2011-06-22', '2012-03-31'),(1, '2012-04-01', '2013-03-31'),(1, '2013-04-01', '2013-06-22')Error in query – Techno Jul 01 '11 at 06:03
  • @Techno: The query seems to be fine. What is the error that you are getting? Please note that it is displaying the query because I've put an "echo" at the end – Abhay Jul 01 '11 at 08:35
  • @Techno: I've removed the "echo" now. Please try to execute $qry now. – Abhay Jul 01 '11 at 08:36
  • I'm still getting Error in query in my PHP Page. But when I'm inserting your generated query into phpmyadmin SQL, the rows are inserted. Would it be possible for you to upload your above tested file on mediafire.com and send me the links? – Techno Jul 01 '11 at 16:23
  • @Techno: here is the file - http://www.mediafire.com/?seh2ntblzxtd29w. Please note that I haven't tested the query in my script but I think it should work fine. By the way, what is the error on your PHP page? – Abhay Jul 01 '11 at 17:03