How do I add this function to my php, codeigniter application

We have a small form php application, that feeds data into a larger application/database.
The larger database is an oracle db, so the 'date' that is sent has to be in oracle format
(we are getting a parse error when the date is sent to the field in the oracle db.)

The smaller form application is written in CodeIgniter which I am just learning.

I think I have found the section of code that bundles the data up in the smaller app and sends it
to the large oracle database.

What I think I understand this code is doing is: checks and counts the fields that are assigned to a field in the larger oracle db, then checks to see if it is a checkbox, radio button, dropdown, etc, if so, seperate the values and send them to the oracle db.

I want to add a check to see if one of the field being send is a DATE field and if so, change the value in that field to oracle formatted date and then send it - somthing like this>>[if ($fldType = FIELD_DATE_FIELD){get_oracle_date($value);}

How do I add this to the above code???? Thanks in advance.
if(count($AcctMgFlds) > 0)
{
foreach($AcctMgFlds as $fld)
{
$fldVal = "";	
//Get Option Text from multi option fields like checkbox, dropdown etc.				
if($fld->getOptionsFromAppFlag() != FIELD_OPTIONS_FROM_App && ($fld->getFieldTypeId() == FIELD_CHECK_BOX || $fld->getFieldTypeId() == FIELD_MULTI_SELECT || $fld->getFieldTypeId() == FIELD_DROP_DOWN || $fld->getFldTypeId() == FIELD_RADIO))
{																		
	$fldVal = $fldMgr->getOptionDisplayText($fld->getId(), str_replace("'","''",$req->getFldVal($fld->getId())));
}
else
{
	$fldVal = str_replace("'","''",$req->getFldVal($fld->getId())); 
}					
$TR[$field->getTRMapIdentifier()] = $fldVal;
}
}

Open in new window

LVL 1
justmelatAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ray PaseurConnect With a Mentor Commented:
I object to deleting this.  A correct answer was given in the tested code snippet, line 101, et seq, at ID:34121611.  Correct versions of format_date and get_oracle_date were given in the code snippet at ID:34158642.  

You wrote "...since I am new to this application, I don't want to change anything..." That is understandable, but that does not mean you have not gotten a good answer to your question.

You wrote "add a check to see if one of the field being sent is a DATE field" - this is what strtotime() does.  And it returns FALSE if the field is not a date.  Install the code snippet attached here and run it.  You will see that the function check_date_format() does not work correctly.  

All of this is clearly presented in the EE article I cited at ID:34158642.  If you read that article you will understand why we use the PHP built-in functions instead of trying to create our own!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
<?php // RAY_temp_justmelat.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');


// COPIED FROM THE "SOLUTION" AT EE
if ( ! function_exists('check_date_format'))
{
      function check_date_format($datevalue)
      {
            if(!preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/",$datevalue))
            {
                  //print "Date format is invalid.";
                  return false;
            }
            else
            {
                  return true;
                  //return get_oracle_date($datevalue);
            }
      }
}


// THE RIGHT WAY TO TEST A DATE
function RAY_check_date_format($d)
{
    if (!strtotime($d))
    {
        echo "<br/>strtotime SAYS DATE STRING $d IS NOT VALID";
    }
    else
    {
        echo "<br/>strtotime SAYS DATE STRING $d IS VALID";
    }
}


// TESTING THE SOLUTION
$date = '99/0/0000';
$news = check_date_format($date);
if ($news) echo "<br/>check_valid_date() SAYS $date IS A VALID DATE";
RAY_check_date_format($date);

$date = '19/47/1947';
$news = check_date_format($date);
if ($news) echo "<br/>check_valid_date() SAYS $date IS A VALID DATE";
RAY_check_date_format($date);

$date = '15-SEP-72';
$news = check_date_format($date);
if ($news) echo "<br/>$date IS A VALID DATE";
if (!$news) echo "<br/>check_valid_date SAYS THE ORACLE DATE $date IS NOT VALID";
RAY_check_date_format($date);

Open in new window

0
 
Ray PaseurCommented:
Let's try this a bite at a time.

What is the required format for the Oracle DATETIME strings?
0
 
justmelatAuthor Commented:
Hi Ray:

The format has to be yyyy-dd-mm - I think, but there is a built in function that will handle that.  I just need to send the value in that field to the function below

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
http://us3.php.net/manual-lookup.php?pattern=get_oracle_date
Where is the code for the get_oracle_date() function?  Is this part of your code?
0
 
justmelatAuthor Commented:
Hi Ray

It is on one of the functions/controller pages in the codeIgniter/php, so it is set to go, I just need to call it correctly.  I can't figure out how to do this in this massive code.
0
 
Ray PaseurCommented:
Is it a stand-alone function or part of a class?  Can you please post the function code?
0
 
justmelatAuthor Commented:
Hi Ray

It oracle date function is attached.
/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
	function get_oracle_date($date)
	{
		$PARTS = explode('/', $date);
	
		if (count($PARTS) < 3)
		{
			return NULL;
		}
	
		switch ($PARTS[0])
		{
			case 01:
				$PARTS[0]='JAN';
				break;
			case 1:
				$PARTS[0]='JAN';
				break;
			case 02:
				$PARTS[0]='FEB';
				break;
			case 2:
				$PARTS[0]='FEB';
				break;
			case 03:
				$PARTS[0]='MAR';
				break;
			case 3:
				$PARTS[0]='MAR';
				break;
			case 04:
				$PARTS[0]='APR';
				break;
			case 4:
				$PARTS[0]='APR';
				break;
			case 05:
				$PARTS[0]='MAY';
				break;
			case 5:
				$PARTS[0]='MAY';
				break;
			case 06:
				$PARTS[0]='JUN';
				break;
			case 6:
				$PARTS[0]='JUN';
				break;
			case 07:
				$PARTS[0]='JUL';
				break;
			case 7:
				$PARTS[0]='JUL';
				break;
			case 08:
				$PARTS[0]='AUG';
				break;
			case 8:
				$PARTS[0]='AUG';
				break;
			case 09:
				$PARTS[0]='SEP';
				break;
			case 9:
				$PARTS[0]='SEP';
				break;
			case 10:
				$PARTS[0]='OCT';
				break;
			case 11:
				$PARTS[0]='NOV';
				break;
			case 12:
				$PARTS[0]='DEC';
				break;
			default:
				return NULL;
		}
	
		return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
	}

Open in new window

0
 
justmelatAuthor Commented:
Oh and it is a part of the date help class
0
 
Ray PaseurCommented:
It never ceases to amaze me - people will write 80 lines of code to do the work of three!

But that aside, can you please post the top few lines of the date help class?

The general way of calling this is to use the class name :: method name($args)
<?php 
error_reporting(E_ALL);
echo "<pre>";
/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
    function get_oracle_date($date)
    {
        $PARTS = explode('/', $date);

        if (count($PARTS) < 3)
        {
            return NULL;
        }

        switch ($PARTS[0])
        {
            case 01:
                $PARTS[0]='JAN';
                break;
            case 1:
                $PARTS[0]='JAN';
                break;
            case 02:
                $PARTS[0]='FEB';
                break;
            case 2:
                $PARTS[0]='FEB';
                break;
            case 03:
                $PARTS[0]='MAR';
                break;
            case 3:
                $PARTS[0]='MAR';
                break;
            case 04:
                $PARTS[0]='APR';
                break;
            case 4:
                $PARTS[0]='APR';
                break;
            case 05:
                $PARTS[0]='MAY';
                break;
            case 5:
                $PARTS[0]='MAY';
                break;
            case 06:
                $PARTS[0]='JUN';
                break;
            case 6:
                $PARTS[0]='JUN';
                break;
            case 07:
                $PARTS[0]='JUL';
                break;
            case 7:
                $PARTS[0]='JUL';
                break;
            case 08:
                $PARTS[0]='AUG';
                break;
            case 8:
                $PARTS[0]='AUG';
                break;
            case 09:
                $PARTS[0]='SEP';
                break;
            case 9:
                $PARTS[0]='SEP';
                break;
            case 10:
                $PARTS[0]='OCT';
                break;
            case 11:
                $PARTS[0]='NOV';
                break;
            case 12:
                $PARTS[0]='DEC';
                break;
            default:
                return NULL;
        }

        return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
    }
}


// TEST WITH AMBIGUOUS DATES
echo get_oracle_date('11/2/2009');
echo PHP_EOL;
echo get_oracle_date('5/3/2009');
echo PHP_EOL;




// THE CONDENSED VERSION OF THIS FUNCTION
function RAY_get_oracle_date($d)
{
    date_default_timezone_set('America/Chicago');
    if (!$t = strtotime($d)) return NULL;
    return strtoupper(date('j-M-y', $t));
}


// TEST WITH AMBIGUOUS DATES
echo RAY_get_oracle_date('11/2/2009');
echo PHP_EOL;
echo RAY_get_oracle_date('5/3/2009');
echo PHP_EOL;

Open in new window

0
 
justmelatAuthor Commented:
Hi Ray

Sorry, it's been a bear of a week.  I have attached the entire date_help.php page.  does this help?

I love your shorter version for the date function, but since I am new to this application, I don't want to change anything just in case.  Knowing my luck, something else will break. LOL.
<?php  
/*
 *--------------------------------------------------------------------------
 * Date Helper Class 
 * @Use Page contains functions related to date  
 *--------------------------------------------------------------------------
 * @author progGroup
 * @created 08/24/2009
 * @version 1.0
 *--------------------------------------------------------------------------
 *
 */

//Code to reject direct script call
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('format_date'))
{
	function format_date($d, $if='y-m-d', $of='m/d/y')
	{
		if ($if == 'y-m-d')
		{
			$DP=explode("-",$d);
			$y=$DP[0];
			$m=$DP[1];
			$d=$DP[2];
		}
	
		if ($if == 'm/d/y')
		{
			$DP=explode("/",$d);
			$y=$DP[2];
			$m=$DP[0];
			$d=$DP[1];
	
		}
		if (($y == '0000') || (empty($y)) )
		{
			return "";
		} else
		{
	
			$R=array($y,$m,$d);
			$F=array("/y/","/m/","/d/");
	
			return preg_replace($F,$R,$of);
		}
	}
}

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
	function get_oracle_date($date)
	{
		$PARTS = explode('/', $date);
	
		if (count($PARTS) < 3)
		{
			return NULL;
		}
	
		switch ($PARTS[0])
		{
			case 01:
				$PARTS[0]='JAN';
				break;
			case 1:
				$PARTS[0]='JAN';
				break;
			case 02:
				$PARTS[0]='FEB';
				break;
			case 2:
				$PARTS[0]='FEB';
				break;
			case 03:
				$PARTS[0]='MAR';
				break;
			case 3:
				$PARTS[0]='MAR';
				break;
			case 04:
				$PARTS[0]='APR';
				break;
			case 4:
				$PARTS[0]='APR';
				break;
			case 05:
				$PARTS[0]='MAY';
				break;
			case 5:
				$PARTS[0]='MAY';
				break;
			case 06:
				$PARTS[0]='JUN';
				break;
			case 6:
				$PARTS[0]='JUN';
				break;
			case 07:
				$PARTS[0]='JUL';
				break;
			case 7:
				$PARTS[0]='JUL';
				break;
			case 08:
				$PARTS[0]='AUG';
				break;
			case 8:
				$PARTS[0]='AUG';
				break;
			case 09:
				$PARTS[0]='SEP';
				break;
			case 9:
				$PARTS[0]='SEP';
				break;
			case 10:
				$PARTS[0]='OCT';
				break;
			case 11:
				$PARTS[0]='NOV';
				break;
			case 12:
				$PARTS[0]='DEC';
				break;
			default:
				return NULL;
		}
	
		return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
	}
}
?>

Open in new window

0
 
Ray PaseurCommented:
I read this from the original post:
(we are getting a parse error when the date is sent to the field in the oracle db.)

But the script posted above parses correctly.  

That said, I can only tell you that this is a technically incompetent piece of programming, and it can be replaced in its entirety with the code snippet, which handles the strings correctly.  This article might be helpful to you.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Notwithstanding the comment on line 4, above, there is no class definition statement in the snippet.  So what question remains now?

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

function RAY_format_date($d, $if='y-m-d', $of='m/d/y')
{
    if (!$t = strtotime($d)) return NULL;
    return date($of, $t);
}

function RAY_get_oracle_date($d)
{
    if (!$t = strtotime($d)) return NULL;
    return strtoupper(date('j-M-y', $t));
}

Open in new window

0
 
justmelatAuthor Commented:
HI Ray


I want to add a check to see if one of the field being sent is a DATE field and if so, change the value in that field to oracle formatted date and then send it - somthing like this>>[if ($fldType = FIELD_DATE_FIELD){get_oracle_date($value);}
0
 
Ray PaseurCommented:
"if one of the field being sent is a DATE field and if so..."

And if not?  The "Date Helper Class" above would likely return a useless value or NULL.
0
 
justmelatAuthor Commented:
Not sure about this.
0
 
Ray PaseurCommented:
Not sure about "this?"  

Please give us a little more to go on if you still need help, thanks.
0
 
justmelatAuthor Commented:
Maybe the long way around, but I think i have my code working

For those who may want to try it the code is attached
0
 
modus_operandiCommented:
Starting auto-close process to implement the recommendations of the participating Expert(s).
 
modus_operandi
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.