Implode an array of multiple variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ben3003

    Implode an array of multiple variables

    I am trying to put a date in my database in the form dd-mm-yyy, as for
    my PHP page is returns 3 variables $date, $month, $year. is ther a way
    to put these together? i have tried imploding them but i can't seem to
    get them into an array!

    here is my code:

    $array = array();
    $array[] = $Day;
    $array[] = $month;
    $array[] = $year;

    $comma_separate d = implode("-", $array);

    Thanks

    Ben

  • Janwillem Borleffs

    #2
    Re: Implode an array of multiple variables

    ben3003 wrote:[color=blue]
    > I am trying to put a date in my database in the form dd-mm-yyy, as for
    > my PHP page is returns 3 variables $date, $month, $year. is ther a way
    > to put these together? i have tried imploding them but i can't seem to
    > get them into an array!
    >
    > here is my code:
    >
    > $array = array();
    > $array[] = $Day;
    > $array[] = $month;
    > $array[] = $year;
    >
    > $comma_separate d = implode("-", $array);
    >[/color]

    What do you mean? You are constructing a string from an array and want
    to construct an array from that string? Using explode instead of implode
    does this, but your intension is unclear to me...


    JW



    Comment

    • ben3003

      #3
      Re: Implode an array of multiple variables

      sorry JW,
      I have 3 variables arrive at this page through a POST from a form. it
      is a date. However it is in the form 3 variables rater than 1 variable.
      the 3 variables are day, month, and year. form here i want to insert
      the values into one filed of my MySQL database in the form dd-mm-yyy.
      Does this make it clear?
      i presumed that an implode function would be best for this.
      Ben

      Comment

      • Colin McKinnon

        #4
        Re: Implode an array of multiple variables

        ben3003 wrote:
        [color=blue]
        > I am trying to put a date in my database in the form dd-mm-yyy, as for
        > my PHP page is returns 3 variables $date, $month, $year. is ther a way
        > to put these together? i have tried imploding them but i can't seem to
        > get them into an array!
        >
        > here is my code:
        >
        > $array = array();
        > $array[] = $Day;
        > $array[] = $month;
        > $array[] = $year;
        >
        > $comma_separate d = implode("-", $array);
        >[/color]

        Ugly, but it should have worked. This is neater:

        $dash_separated = implode("-", array($day,$mon th,$year));

        but this is better:

        $dash_separated = sprintf("%02d-%02d-%04d",$day,$mon th,$year);

        C.

        Comment

        • BKDotCom

          #5
          Re: Implode an array of multiple variables

          Perhaps register-globals is off (like it shoud be)?
          $dash_separated = implode("-",
          array($_POST['day'],$_POST['month'],$_POST['year']));

          Comment

          • Jan Pieter Kunst

            #6
            Re: Implode an array of multiple variables

            ben3003 wrote:
            [color=blue]
            > I have 3 variables arrive at this page through a POST from a form. it
            > is a date. However it is in the form 3 variables rater than 1 variable.
            > the 3 variables are day, month, and year. form here i want to insert
            > the values into one filed of my MySQL database in the form dd-mm-yyy.[/color]


            Is there a compelling reason to not store the date in the standard SQL
            format 'YYYY-MM-DD'?

            $datestring = "$year-$month-$Day";

            Or "$Day-$month-$year" if you really want that order.

            JP

            --
            Sorry, <devnull@cauce. org> is a spam trap.
            Real e-mail address unavailable. 5000+ spams per month.

            Comment

            • Michael Fesser

              #7
              Re: Implode an array of multiple variables

              .oO(Jan Pieter Kunst)
              [color=blue]
              >Is there a compelling reason to not store the date in the standard SQL
              >format 'YYYY-MM-DD'?
              >
              >$datestring = "$year-$month-$Day";[/color]

              It could be necessary to use sprintf() to add leading zeros.

              Micha

              Comment

              • ben3003

                #8
                Re: Implode an array of multiple variables

                ok well i am using:
                $day = $_POST['StartDay'];
                $month = $_POST['StartMn'];
                $year = $_POST['StartYr'];

                $dash_separated = sprintf("%d-%d-%d",$day,$month ,$year);

                echo $dash_separated ;

                and for some reason i am getting the folowing output:
                13-0-0
                This should be:
                13-01-2001
                any one?
                Ben

                Comment

                • ben3003

                  #9
                  Re: Implode an array of multiple variables

                  $day = $_POST['StartDay'];
                  $month = $_POST['StartMn'];
                  $year = $_POST['StartYr'];

                  $dash_separated = sprintf("%d-%d-%d",$day,$month ,$year);

                  echo $dash_separated ;

                  this currently is returning
                  13-00-00
                  when i would like
                  13-01-2005

                  any ideas? thanks ben

                  Comment

                  Working...