SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploading wrong date format

    Hi all,

    I have a form and as part of it the date its created is uploaded too.

    With that data in the database an excel file is generated and the data has to be in a certain way for their software to accept it, and one of those is the date created bit.

    So in my code I am doing this:

    Code:
    $current_time = date ('Y-m-d H:i:s');
    And its being uploaded to the database with the query below:

    Code:
    $sql = mysql_query("INSERT INTO hazzard (Name, Employerofperson, Contactphone, Location, DateOccured, ProductionZone, NearMissDetails, Anyimmediateactions) VALUES ('".mysql_real_escape_string($_SESSION['name_A'])."','".mysql_real_escape_string($_SESSION['companies_A'])."','".mysql_real_escape_string($_SESSION['numbers_A'])."','".mysql_real_escape_string($location)."','$current_time','$ProductionZone','".mysql_real_escape_string($detail)."','".mysql_real_escape_string($feedback)."')") or die (mysql_error());
    Which gives me an output like this: 2013-08-29 16:13:31

    I need to keep that as it goes, so I need to create another date created filed but htis time so that it outputs like this: 29/08/2013

    With creating another field in the database to accomodate this, do I set it a certain way and how also do I create the date that way ready to go in the update script.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    1) There is no reason to store the same value twice in a db, even if its formatted differently. You can format it how you need to when you use it within a query.
    2) You can create a column in the db with default value of the current date and time, this way you dont even have to worry about inserting the date value.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So create the column and make it create automatically is it.

  4. #4
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the current column date field showing as :2013-08-29 18:20:42

    How would I re-format that on the page to output as 29-August-13

    Thats basically what I need, rather than like you say adding another column in.

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by multichild View Post
    How would I re-format that on the page to output as 29-August-13
    You can format the date as part of your SELECT query when you're fetching the data for output:

    Code MySQL:
    SELECT DATE_FORMAT(DateOccured, '%e-%M-%y') as my_date FROM hazzard

  6. #6
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner,

    That was exactly what I needed, and it worked perfectly.

    Cheers

  7. #7
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner,

    Just in case the request is made over the weekend, how can your script be adapted to also include the time, just added onto the end of what you suggested.

    Cheers

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by multichild View Post
    Just in case the request is made over the weekend, how can your script be adapted to also include the time, just added onto the end of what you suggested.
    You'd just need to add extra formatting codes.. you can find a list of them here: http://dev.mysql.com/doc/refman/5.1/...on_date-format - it's possible to output the date/time in just about any format you could need.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •