1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Display timestamp as UK format date on Select?

Discussion in 'Databases' started by mkultron, Dec 15, 2011.

  1. #1
    Hi guys,

    I'm saving user input as a timestamp to my database but when I select it I want the date to display in the UK format date day/month/year. Ideally I'd like to display it like this "15th December 2011" using PHP. It is not the current time but a time in the future the user selects for an event. Is this something I can do from a timestamp or do I need to save my data some other way? e.g. as a datetime. Also, I want to display a time but like "7:00 am" and have it saved to my database, what would be the best way to do this?

    Any help would be greatly appreciated
    Thanks
    MK
     
    mkultron, Dec 15, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Leave the query and the database format alone. Use php to format the output for the user. Can you post the code where the date is displayed?

    You should use something like this for the date.

    echo date('jS F Y',strtotime($database_timestamp));

    http://php.net/manual/en/function.date.php
     
    jestep, Dec 15, 2011 IP
  3. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thank you.

    I added the following timestamp to test:

    2010-03-04 10:00:00

    But using the following brings all the dates (above included) up as 1st
    January 1970, so its the correct format but incorrect date.

    echo date('jS F Y',strtotime($eventdate));

    Do I need to be using something like "destroy_date" here? I tried something
    like this but it just gives today's date:

    $eventdate = new DateTime( $row['destroy_date'] );
    echo $eventdate->format( 'jS F Y' );

    MK
     
    mkultron, Jan 17, 2012 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Is it throwing an error? DateTime should be able to handle pre 1970 dates.
     
    jestep, Jan 17, 2012 IP
  5. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    I'm not getting an error, just the January 1st 1970 date. I slightly tweaked the line to "echo date("jS F Y", strtotime($row['eventdate']));", but again, no luck.
     
    mkultron, Jan 17, 2012 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Can you echo out the $row['destroy_date'] before using it with DateTime.
     
    jestep, Jan 17, 2012 IP
  7. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    Like this "echo $eventdate = $row['destroy_date'];" ? I've tried but no change.
     
    mkultron, Jan 17, 2012 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    Do this:

    echo $row['destroy_date'];
    $eventdate = new DateTime( $row['destroy_date'] );
     
    jestep, Jan 17, 2012 IP
  9. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Hmm still no luck. Probably a stupid question but might it be that I'm storing the information as a timestamp and not a datetime?
     
    mkultron, Jan 17, 2012 IP
  10. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #10
    Mysql timestamp and datetime fields store the same way. They should both work with this function.

    Did this not output anything?
    echo $row['destroy_date'];
    $eventdate = new DateTime( $row['destroy_date'] );

    I was trying to see the original value you are passing to DateTime().
     
    jestep, Jan 17, 2012 IP
  11. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    That seems to be outputting nothing at all, when I simply echo the eventdate row on another page I see dates like "2012-03-04 11:00:00". Confused!
     
    mkultron, Jan 18, 2012 IP
  12. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    Any idea why this may be happening? I'm literally pasting into a box right now something like "2012-03-04 10:00:00" in order to create the date, storing it as a timestamp in the database table and pulling it into another page and getting that january 1970 mumbo jumbo when I try with the UK date format.
     
    mkultron, Jan 19, 2012 IP
  13. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #13
    If I use these:

    
    
    $date = "2012-03-04 10:00:00";
    
    $dt = new DateTime($date);
    
    echo $dt->format('jS F Y'); //4th March 2012
    
    $date = "1925-03-04 10:00:00";
    
    $dt = new DateTime($date);
    
    echo $dt->format('jS F Y'); //4th March 1925
    
    
    PHP:
    It has to be something to do with the value you are passing to DateTime.

    Can you post the actual code when you instantiate DateTime?
     
    jestep, Jan 19, 2012 IP
  14. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #14
    I tried that and I'm not displaying today's date. Minus some pagination I've got going on this is is about all I've got to pull and display the data:

    $result = mysql_query("SELECT * FROM stuff.events ORDER BY eventdate ASC");

    echo "<br />";
    echo mysql_result($result, $i, 'eventvenue');
    echo ", ";

    $dt = new DateTime($eventdate);

    echo $dt->format("jS F Y");
     
    mkultron, Jan 20, 2012 IP
  15. mkultron

    mkultron Greenhorn

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #15
    Sorry, that (which I just posted) does bring up today's date but not the time I specified which displays fine without trying the UK formatting. It seems like we've tried everything.

    Not sure if this'll be any use and sorry if I'm reiterating but I really need to get this solved. This is what i've got for the eventdate field in the database. obviously because I want the user to update the timestamp themselves right now i've just got to type the correct format 0000-00-00 00:00:00 which displays fine until I try to echo it out (post before this one). I've been chopping and changing so it's possible something could have gone wrong along the way.


    field - eventuate
    type - timestamp
    length/values - blank
    default - current_timestamp
    collation - blank
    attributes - on update CURRENT_TIMESTAMP
    null - blank
    auto_increment - blank

    thank you so much for your help.
     
    mkultron, Jan 22, 2012 IP
  16. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #16
    strtotime() takes a string argument, not a timestamp argument. It's probably seeing your timestamp as a null string, which is 1-1-1970.
     
    Rukbat, Feb 25, 2012 IP