turpentyne Posted September 4, 2012 Share Posted September 4, 2012 I'm trying to figure out how to get this to generate the date field pulled from a query that pulls a datetime field called "date". But It's just giving me some default: 12/31/69 7:00 PM $Output .= sprintf ($ExpandImageTemplate, htmlspecialchars ($row['title']),htmlspecialchars ($row['content']),htmlspecialchars ($row['img_url_small']),htmlspecialchars ($row['img_url_large']),htmlspecialchars ($row['deck']),date("m/d/y g:i A",($row['date']))); Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/ Share on other sites More sharing options...
requinix Posted September 4, 2012 Share Posted September 4, 2012 Without looking at your code, if you're getting that date it means that the "timestamp" value you gave to some date function (like date() or strtotime()) is invalid. Or very small. Like date("Y-m-d H:i:s", "September 4th 2012, 12:30 am") // 1969-12-31 HH:00:00 Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375075 Share on other sites More sharing options...
salathe Posted September 4, 2012 Share Posted September 4, 2012 You have date("m/d/y g:i A",($row['date'])). The second argument to date is expected to be an integer containing the Unix timestamp for the date and time you want to format. You're most likely passing it an already formatted date string. Depending on the format of this date string, it might be sufficient to simply use strtotime to convert it into a Unix timestamp before re-formatting with date. date('m/d/y g:i A', strtotime($row['date'])) However, strtotime cannot make sense of every possible date/time string. If the above does not work, show us the value for $row['date'] as there are other alternatives available for when strtotime does not work. Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375082 Share on other sites More sharing options...
turpentyne Posted September 5, 2012 Author Share Posted September 5, 2012 I tried your suggestion and still get the same thing. That row in MySQL is just: 2012-09-02 00:00:00 a datetime field. Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375352 Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 Can you do a var_dump ($row['date']) just before the date conversion? I think the answer should come to light if you do. Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375354 Share on other sites More sharing options...
turpentyne Posted September 5, 2012 Author Share Posted September 5, 2012 dear lord! False alarm! It helps if I include that field in my query!!! problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375355 Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 Hehe, suspected as much. Glad we could help out. Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375358 Share on other sites More sharing options...
salathe Posted September 5, 2012 Share Posted September 5, 2012 Glad we could help out. I'm not entirely sure we did. Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375369 Share on other sites More sharing options...
Christian F. Posted September 5, 2012 Share Posted September 5, 2012 He figured out what the problem was, and corrected it. I'd say that qualifies as help. Of course, the problem wasn't what he thought it was, and thus was asking for, but... Quote Link to comment https://forums.phpfreaks.com/topic/267978-sprintf-output-for-date-format/#findComment-1375373 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.