FormatDateTime Revisited

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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'] .

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.


Re: FormatDateTime Revisited

PHP has some great date functions for formatting dates however you
could possibly want.  First take a look at 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

So basically you would want to do:

echo date("Y-m-d H:i:s", strtotime("December 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.

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'] .

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_Comments) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['gigTitle'], "text"),
                       date("Y-m-d", ($dateMerge)),
                       GetSQLValueString($timeMerge, "text"),
                       GetSQLValueString($_POST['comments'], "text"));

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

Adam Plocher wrote:
Quoted text here. Click to load it

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($dateMerge)) and I believe
it'll work.

Re: FormatDateTime Revisited

John K top-posted (previous posts snipped):
Quoted text here. Click to load it

          date("Y-m-d", mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'])),

Quoted text here. Click to load it

If you're posting through Google read <

Re: FormatDateTime Revisited

John K wrote:
Quoted text here. Click to load it

    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 <

Site Timeline