I have a dynamic array $v whose contents change according to a form being submitted. Some forms might have a date field and others may not. For all forms I’m using the same array $v to store values in a key=>value pair and then insert it into the table. So sometimes the array may look like
$v = array('patron_name'=>'some value',
'place' => 'again some value',
'pin' => 'blah blah')
and at other times it may look like
$v = array('joomla'=>'some value again',
'date_applied' => '23/04/2012',
)
As seen above, the contents vary from form to form. What I’m looking for is to first check if $v contains a value in the form of dd/mm/yyy and if it does, change it to yyyy-mm-dd format to insert into table. I can handle the conversion part, but I’m stuck at determining if $v contains a date or not.
So basically I’m intending to proceed in this way:
if(in_array('date in dd/mm/yyyy format', $v))
{
// change it to yyyy-mm-dd
}
// and then insert into table
$flag = insert($tablename, $v);
Any help much appreciated. Thanks in advance
Instead of
in_array, usepreg_replace_callback. Pass the array to it and match for the date format pattern. If the pattern matches, replace it with the callback.Example (demo):
On a sidenote, since Y-m-d contains the same values as d/m/Y, you don’t strictly need to use a callback but can just use
preg_replace(demo):Using a callback would allow you to use an arbitrary date format though.