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
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
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
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.
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?
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().
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!
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.
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?
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");
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.
strtotime() takes a string argument, not a timestamp argument. It's probably seeing your timestamp as a null string, which is 1-1-1970.