ajannick Posted June 4, 2007 Share Posted June 4, 2007 Hi All, I'm having trouble formating dates returned from a database successfully, the code below does the formating but makes all the entries in the database share todays date and time: <?php while($row=mysql_fetch_array($sql)) { $row['dateField'] = date("D M jS Y H:i"); echo"<tr>\n"; echo"<td background='graphics/cloud.jpg'><center><span class='blackA'> ".$row['dateField']."</span></center></td>\n"; echo"</tr>\n"; } ?> This formats the date how I want it but then all the entries are retuned with the todays date and time values! Any Ideas? N. Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/ Share on other sites More sharing options...
redarrow Posted June 4, 2007 Share Posted June 4, 2007 and the entry in the database is set as a time stamp is that correct. Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267940 Share on other sites More sharing options...
Wildbug Posted June 4, 2007 Share Posted June 4, 2007 $row['dateField'] = date("D M jS Y H:i"); That's because you're setting it to today's date and time at the above line of code. Just put it in the echo line: echo .... date("D M jS Y H:i",$row['dateField']) .... See date() for syntax. It needs to be a UNIX timestamp. Also consider that you can format the date in your SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267941 Share on other sites More sharing options...
ajannick Posted June 4, 2007 Author Share Posted June 4, 2007 Yes Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267943 Share on other sites More sharing options...
redarrow Posted June 4, 2007 Share Posted June 4, 2007 try this ok. <?php while($row=mysql_fetch_array($sql)) { $dateField = date("D M jS Y H:i",$row['dateField']); echo"<tr>\n"; echo"<td background='graphics/cloud.jpg'><center><span class='blackA'> $dateField</span></center></td>\n"; echo"</tr>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267944 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 when I put it in the echo line it comes up with all dates 1st JAN 1970 00:33? Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267947 Share on other sites More sharing options...
redarrow Posted June 5, 2007 Share Posted June 5, 2007 post your timestamp entry your testing ok. Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267948 Share on other sites More sharing options...
kenrbnsn Posted June 5, 2007 Share Posted June 5, 2007 The second parameter to the date() function must be an integer UNIX timestamp. You need to do: <?php $dateField = date("D M jS Y H:i",strtotime($row['dateField'])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267949 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 SELECT "UNIX_TIMESTAMP(dateField) as timestamp "; Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267950 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 This works just about! The dates are now all correct but the times are all 7 hours infront? Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267952 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 sorry 7 behind Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267953 Share on other sites More sharing options...
redarrow Posted June 5, 2007 Share Posted June 5, 2007 i think this is correct. <?php $H = date("H",strtotime($row['dateField'])-7); $dateField = date("D M jS Y $H:i",strtotime($row['dateField'])); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267956 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 Strangely that knocks it 8 hours back? Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267958 Share on other sites More sharing options...
redarrow Posted June 5, 2007 Share Posted June 5, 2007 use -6 then ok Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267960 Share on other sites More sharing options...
ajannick Posted June 5, 2007 Author Share Posted June 5, 2007 result stays the same! Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-267962 Share on other sites More sharing options...
Wildbug Posted June 5, 2007 Share Posted June 5, 2007 As I said earlier, you could also format the date/time directly in MySQL before retreiving the value and just print it like any other value. // In your SQL query: ..., DATE_FORMAT(dateField,"%a %b %D %Y %H:%i") AS formatted_datefield, ... // In your PHP code: echo $row['formatted_datefield']; (Are your date values stored as GMT in the database?) Quote Link to comment https://forums.phpfreaks.com/topic/54194-formating-dates-is-making-each-entry-return-todays-date/#findComment-268398 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.