Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

check date format from csv file insertion to DB

I am using PHP. I allow users to upload records using a csv file, which is directly inserted into MySql DB after checking the field contents to be non-empty, integer etc.
Is there a way to check if the date is in the correct format in the csv file before insertions? i used is_date() function but it doesn't seem to exist. i want to check the following:
1.  if date field is in mm-dd-yyyy or mm/dd/yyyy (notice the separator), i want to change it to MySql format ie yyyy-mm-dd before inserting
2.   if date field is in yyyy-mm-dd, then it should proceed with insertion.
MUST check that field is in MySql format & if not then if possible then change it to that before insertion. I need it all to be done using PHP. Can someone please help. Thanks in advance
0
ARC_UM
Asked:
ARC_UM
  • 2
  • 2
1 Solution
 
mstrelanCommented:
try strtotime() and date() functions
www.php.net/strtotime
www.php.net/date

try inserting something like date('Y-m-d', strtotime($theVarFromCSV));

strtotime converts basically any string to an int (the number of seconds since 1/1/1970 at midnight)
date formats an integer into whatever format you choose
0
 
ARC_UMAuthor Commented:
I didn't had a chance to see your suggestion since i was away. will try & let you know how it goes. Thanks
0
 
ARC_UMAuthor Commented:
will date('Y-m-d', strtotime($theVarFromCSV)) check fields in the form dd-mm-yyyy or dd/mm/yyyy or yyyy-mm-dd or yyyy/mm/dd & convert it to yyyy-mm-dd in MySql format for insertion in DB?
Will the date function also make sure that dd is between 1-31, mm is within 1-12 & so on?
0
 
mstrelanCommented:
yes exactly this
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now