FormatDateTime Revisited

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

    #1

    FormatDateTime Revisited

    I posted on this a week or so ago and found a resolution in that
    instance by using DATE_FORMAT in my sql string to display the format I
    was looking for. This time I need to format the date going "into" the
    database. In ASP/VBScript I just use "FormatDateTime " to convert the
    users input into a palatable MySQL "date" datatype.

    Here's what I need...
    The user inputs a date by selecting dropboxes for "month", "day", and
    "year". I did this to put them together.

    $dateMerge = $_POST['year'] . "-" . $_POST['month'] . "-" .
    $_POST['day'] ;
    $timeMerge = $_POST['Time_Hour'] . ":" . $_POST['Time_Minute'] .
    $_POST['Time_AMPM'];

    Again, in ASP I would just format the input using "FormatDateTime " and
    that's it. How do I achieve this in PHP. PHP is "very" new to me and
    I'm a little thrown by the differences.

    Thanks!

  • Adam Plocher

    #2
    Re: FormatDateTime Revisited

    PHP has some great date functions for formatting dates however you
    could possibly want. First take a look at www.php.net/date for a list
    of formats.

    Notice that the second (optional) parameter to the date function is a
    timestamp parameter. You must convert your date/time into a unix
    timestamp but this can easily be achieved with the strtotime() function
    (www.php.net/strtotime).

    So basically you would want to do:

    echo date("Y-m-d H:i:s", strtotime("Dece mber 25, 2005 11:55 PM"));
    // this should return 2005-12-25 23:55:00 - this is the format I
    usually use when inserting into MySQL

    Note that strtotime should be able to take in just about ANY format of
    date and time.

    Comment

    • Pedro Graca

      #3
      Re: FormatDateTime Revisited

      John K wrote:[color=blue]
      > I need to format the date going "into" the database.[/color]

      mysql> create table foo (id int primary key auto_increment, t1 datetime, t2 timestamp);
      Query OK, 0 rows affected (0.03 sec)

      mysql> insert foo (t1, t2) values (20060117201703 , 20060117201703) ;
      Query OK, 1 row affected (0.05 sec)

      mysql> select * from foo;
      +----+---------------------+---------------------+
      | id | t1 | t2 |
      +----+---------------------+---------------------+
      | 1 | 2006-01-17 20:17:03 | 2006-01-17 20:17:03 |
      +----+---------------------+---------------------+
      1 row in set (0.00 sec)

      --
      If you're posting through Google read <http://cfaj.freeshell. org/google>

      Comment

      • John K

        #4
        Re: FormatDateTime Revisited

        I have the date and time fields separate at the moment. this is what
        I'm trying to do...


        $dateMerge = $_POST['month']. " " . $_POST['day'] . ", " .
        $_POST['year'] ;
        $timeMerge = $_POST['Time_Hour'] . ":" . $_POST['Time_Minute'] .
        $_POST['Time_AMPM'];

        if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "gigAdd"))
        {
        $insertSQL = sprintf("INSERT INTO tbl_gigs (fld_gig_Title,
        fld_gig_Date, fld_gig_Time, fld_gig_Comment s) VALUES (%s, %s, %s, %s)",
        GetSQLValueStri ng($_POST['gigTitle'], "text"),
        date("Y-m-d", ($dateMerge)),
        GetSQLValueStri ng($timeMerge, "text"),
        GetSQLValueStri ng($_POST['comments'], "text"));

        it's not working, the date is still messed up.


        Adam Plocher wrote:[color=blue]
        > PHP has some great date functions for formatting dates however you
        > could possibly want. First take a look at www.php.net/date for a list
        > of formats.
        >
        > Notice that the second (optional) parameter to the date function is a
        > timestamp parameter. You must convert your date/time into a unix
        > timestamp but this can easily be achieved with the strtotime() function
        > (www.php.net/strtotime).
        >
        > So basically you would want to do:
        >
        > echo date("Y-m-d H:i:s", strtotime("Dece mber 25, 2005 11:55 PM"));
        > // this should return 2005-12-25 23:55:00 - this is the format I
        > usually use when inserting into MySQL
        >
        > Note that strtotime should be able to take in just about ANY format of
        > date and time.[/color]

        Comment

        • Adam Plocher

          #5
          Re: FormatDateTime Revisited

          John, you will need to use strtotime to convert your $dateMerge into a
          timestamp (before passing in to date()). Simply change date("Y-m-d",
          ($dateMerge)) to date("Y-m-d", strtotime($date Merge)) and I believe
          it'll work.

          Comment

          • Pedro Graca

            #6
            Re: FormatDateTime Revisited

            John K top-posted (previous posts snipped):[color=blue]
            > I have the date and time fields separate at the moment. this is what
            > I'm trying to do...
            >
            >
            > $dateMerge = $_POST['month']. " " . $_POST['day'] . ", " .
            > $_POST['year'] ;
            > $timeMerge = $_POST['Time_Hour'] . ":" . $_POST['Time_Minute'] .
            > $_POST['Time_AMPM'];
            >
            > if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "gigAdd"))
            > {
            > $insertSQL = sprintf("INSERT INTO tbl_gigs (fld_gig_Title,
            > fld_gig_Date, fld_gig_Time, fld_gig_Comment s) VALUES (%s, %s, %s, %s)",
            > GetSQLValueStri ng($_POST['gigTitle'], "text"),
            > date("Y-m-d", ($dateMerge)),[/color]

            date("Y-m-d", mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'])),
            [color=blue]
            > GetSQLValueStri ng($timeMerge, "text"),
            > GetSQLValueStri ng($_POST['comments'], "text"));
            >
            > it's not working, the date is still messed up.[/color]

            --
            If you're posting through Google read <http://cfaj.freeshell. org/google>

            Comment

            Working...