check date format from csv file insertion to DB

Posted on 2009-04-23
Last Modified: 2013-12-12
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
Question by:ARC_UM
    LVL 10

    Accepted Solution

    try strtotime() and date() functions

    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

    Author Comment

    I didn't had a chance to see your suggestion since i was away. will try & let you know how it goes. Thanks

    Author Comment

    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?
    LVL 10

    Expert Comment

    yes exactly this

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now