Alexhoward Posted June 16, 2008 Share Posted June 16, 2008 Good morning PHP people! I am using an auto timestamp in mysql 5. This gets added automatically when the data is inserted It's saved as: 2008-06-16 09:09:53 and i would like to change it to: Mon 16 June problem is that when i use the normal php date formatting method it comes out with some bizare results... has anyone come across this before...? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/ Share on other sites More sharing options...
AndyB Posted June 16, 2008 Share Posted June 16, 2008 Leave it alone in the database - that format is the best. Change how it is displayed by your scripts after being retrieved from the database. The date() function allows all kinds of personal preference date formats. Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566383 Share on other sites More sharing options...
zenag Posted June 16, 2008 Share Posted June 16, 2008 select date_format( date fieldname , '%a %b %Y' ) as date from.... Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566389 Share on other sites More sharing options...
Alexhoward Posted June 16, 2008 Author Share Posted June 16, 2008 Hi Guys, Thanks for the replies! here's my code: (just a test and an extract of the full thing) <?php include("config.php"); //connect to the mysql server $link = mysql_connect($host, $db, $pass) or die ("Could not connect to mysql because ".mysql_error()); //select the database mysql_select_db($db) or die ("Could not select database because ".mysql_error()); $query2=" SELECT * FROM mail "; $result2=mysql_query($query2); echo mysql_error(); while($row = mysql_fetch_array($result2)) { $from = $row['sender']; $title = $row['title']; $body = $row['body']; $date = $row['date']; $ref = $row['ref']; } echo "$date</br>"; echo date("D-d-M-Y", $date); ?> problem being that my output is: 2008-06-16 09:26:23 Thu-01-Jan-1970 any ideas why this is happening...? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566399 Share on other sites More sharing options...
zenag Posted June 16, 2008 Share Posted June 16, 2008 echo date("D-M-Y", $date); Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566400 Share on other sites More sharing options...
Alexhoward Posted June 16, 2008 Author Share Posted June 16, 2008 Hi zenag That just gives me 2008-06-16 09:26:23 Thu-Jan-1970 or have i missed something...? Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566403 Share on other sites More sharing options...
zenag Posted June 16, 2008 Share Posted June 16, 2008 sorry ...i ve misunderstood.... try this ...it satisfies ur expectations... while($row = mysql_fetch_array($result2)) { $date = $row['created_at']; $ref = $row['ref'];$date=date('D-M-Y'); Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566409 Share on other sites More sharing options...
zenag Posted June 16, 2008 Share Posted June 16, 2008 echo $date .. Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566410 Share on other sites More sharing options...
Alexhoward Posted June 16, 2008 Author Share Posted June 16, 2008 Excellent! That seems to work a treat! Thanks Just a quick question thou, as the two variables have the same name i.e. $date = $row['date']; $date=date('D d/M/Y'); and i'm echo-ing $date is it just combining them...? Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566413 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2008 Share Posted June 16, 2008 The date() function expects the second parameter to be a UNIX timestamp (integer), you are passing it a string, so it gives you back the zero date (1/1/1970). You need to use the function strtotime() to convert the string date stamp to a UNIX timestamp before you use it with the date() function. <?php echo "$date</br>"; echo date("D-d-M-Y", strtotime($date)); ?> or use zenag's method. Ken Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566415 Share on other sites More sharing options...
Alexhoward Posted June 16, 2008 Author Share Posted June 16, 2008 Cheers kenrbnsn. both methods work beautifully Just out of interest i take it it is best practice to use: echo date("D-d-M-Y", strtotime($date)); Quote Link to comment https://forums.phpfreaks.com/topic/110401-solved-how-to-format-mysql-5-timestamp/#findComment-566423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.