I have a very simple question. I need to convert the date separator before inserting the date into the DB. Right now, I have 01/01/2006 and I want 01-01-2006. The dates are coming from a CSV and so are an array element. I've tried strrpos and a few other functions trying to just replace the slashes with hypens and that's not working (I think maybe because it's an array and those functions don't work on arrays?)

Could someone post example code for something like this?

I have an array called $items[3] that holds the date with the separator mentioned above. I'm sure there are several ways to do it in both php and with mysql functions but I've been stuck on it for a while and REALLY need to get through this for a project I'm working on right now.

TIA,

Adam

    Have you tried:

    $items[3] = str_replace("/","-", $items[3]);
    

      Worked perfectly. Thank you. Now, one more question. Mysql wants the dates as yyyy-mm-dd. I have one date that is mm-dd-yyyy and one that is mm-dd-yy. How to convert those properly? Thanks for your help 🙂

        That's going to be hard, because if you use a function such as [man]strtotime/man, there is some ambiguity involved in which set is the month and which is the day. Is 06-03-2006 in the month of June or March?

        What you might need to do is create functions that convert the different types of dates, and run each value through it's matching function. For example:

        function mdy_to_ymd($date) {
             $date = explode('-', $date);
        
         if(count($date) != 3) {
              die('Invalid date passed: ' . implode('', $date));
         } else {
              return $date[2] . '-' . $date[0] . '-' . $date[1];
         }
        }

        would be one function. I'm sure you can create the second based on that example.

        Point is, you'd have to know which format the date is in and run it through the correct function. Also, if you want the functions to work with both - and / as separators, you'd have to use the [man]split/man command instead of [man]explode/man, such as:

             $date = split('[/-]', $date);
          bradgrafelman wrote:

          That's going to be hard, because if you use a function such as [man]strtotime/man, there is some ambiguity involved in which set is the month and which is the day. Is 06-03-2006 in the month of June or March?

          What you might need to do is create functions that convert the different types of dates, and run each value through it's matching function. For example:

          function mdy_to_ymd($date) {
               $date = explode('-', $date);
          
           if(count($date) != 3) {
                die('Invalid date passed: ' . implode('', $date));
           } else {
                return $date[2] . '-' . $date[0] . '-' . $date[1];
           }
          }

          would be one function. I'm sure you can create the second based on that example.

          Point is, you'd have to know which format the date is in and run it through the correct function. Also, if you want the functions to work with both - and / as separators, you'd have to use the [man]split/man command instead of [man]explode/man, such as:

               $date = split('[/-]', $date);

          is it working if I have a text input.
          A date generate from calendar : 18-July-2005

          how do I insert the date into MySQL database?

            Is the month always going to be a textual reference? If so, I would build an array of months with the key being the month's number and the value being the month's name, and use [man]array_search[/man] to retrieve the number.

            If not, you would have to add an if statement, using [man]is_numeric/man on the month parameter, and if it is numeric then simply continue with the code I gave above, if not, use the array method I've outlined in this post.

              Write a Reply...