doubledee Posted September 3, 2011 Share Posted September 3, 2011 I am getting this error when I output a formatted date... Warning: date() expects parameter 2 to be long, string given in I am using DATETIME. What do I need to do to fix things? Here is my code... echo '<p class="commentDate">' . date($createdOn, 'g:ia') . ' on ' . date($createdOn, 'M j, Y') . '</p>'; Debbie Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2011 Share Posted September 3, 2011 The error is pretty self explanatory. The second parameters is expected to be a "long" (i.e. long integer - in this particular case a timestamp). It looks like you have the format string and the timestamp transposed. http://us.php.net/manual/en/function.date.php string date ( string $format [, int $timestamp = time() ] ) Quote Link to comment Share on other sites More sharing options...
doubledee Posted September 3, 2011 Author Share Posted September 3, 2011 The error is pretty self explanatory. The second parameters is expected to be a "long" (i.e. long integer - in this particular case a timestamp). It looks like you have the format string and the timestamp transposed. http://us.php.net/manual/en/function.date.php string date ( string $format [, int $timestamp = time() ] ) But will the MySQL data-type "DATETIME" work? I don't understand what a "long" has to do with a Date/Time? Also, when I fixed my transpose, now I get this... Notice: A non well formed numeric value encountered Here is my updated code... echo '<p class="commentDate">' . date('g:ia', $createdOn) . ' on ' . date('M j, Y', $createdOn) . '</p>'; Debbie Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 If this is coming from a DB query, why not just format it as needed directly in the query with MySQL's DATE_FORMAT() function, then just display it? Quote Link to comment Share on other sites More sharing options...
doubledee Posted September 3, 2011 Author Share Posted September 3, 2011 If this is coming from a DB query, why not just format it as needed directly in the query with MySQL's DATE_FORMAT() function, then just display it? Is that how you are supposed to do it? Isn't that more taxing on your system including the database? I just assumed you should store all Date/Time in a standard format in the database like ('YYYY-MM'DD HH:MM;SS') and then format things in PHP as needed? Debbie Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2011 Share Posted September 3, 2011 MySQL and PHP timestamps are not compatible. A PHP timestamp is a long integer that represent the number of seconds since the epoch (Jan 1, 1970). A MySQL timestamp is in the format "YYYY-MM-DD HH:MM:SS". The PHP date() function expects a PHP timestamp, not a MySQL timestamp. There are various ways to convert from one to the other - either in PHP or MySQL. You can use MySQL's date format as Pikachu suggested, but I'm more comfortable using PHP's date function. And, PHP has a very simple wy of converting a MySQL date/timestamp to a PHP timestamp: strtotime(). echo '<p class="commentDate">' . date('g:ia', strtotime($createdOn)) . ' on ' . date('M j, Y', strtotime($createdOn)) . '</p>'; Quote Link to comment Share on other sites More sharing options...
doubledee Posted September 3, 2011 Author Share Posted September 3, 2011 MySQL and PHP timestamps are not compatible. A PHP timestamp is a long integer that represent the number of seconds since the epoch (Jan 1, 1970). A MySQL timestamp is in the format "YYYY-MM-DD HH:MM:SS". The PHP date() function expects a PHP timestamp, not a MySQL timestamp. There are various ways to convert from one to the other - either in PHP or MySQL. You can use MySQL's date format as Pikachu suggested, but I'm more comfortable using PHP's date function. And, PHP has a very simple wy of converting a MySQL date/timestamp to a PHP timestamp: strtotime(). echo '<p class="commentDate">' . date('g:ia', strtotime($createdOn)) . ' on ' . date('M j, Y', strtotime($createdOn)) . '</p>'; Well, your above fixed things. Thanks! Probably a much larger topic, but is it okay to use DATETIME in my table? (I chose it because it has benefits like being able to use it on multiple fields and it not dying in a few decades?!) Also, if I did want to do it Pikachu2000's way, I'm a little stuck on the formatting... I want my date to look like this: 7:05pm on July 10, 2011 Here is my code which isn't complete... $q2 = 'SELECT m.first_name, DATE_FORMAT(c.created_on, '%l:%i%p on %b %e, %Y'), c.body, c.status FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?'; Debbie Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 That looks right, Maybe just add a field alias so you can easily refer to it in the results. And yes, you should be using DATETIME because there are so many native MySQL functions for comparing/manipulating DATETIME values. Quote Link to comment Share on other sites More sharing options...
doubledee Posted September 3, 2011 Author Share Posted September 3, 2011 That looks right, Maybe just add a field alias so you can easily refer to it in the results. But notice the work ON nested in there... Doesn't that need special treatment? And I also have a quote issue because I am doing this in PHP. Do I add backslashes to things like: $q2 = 'SELECT m.first_name, DATE_FORMAT(c.created_on, \'%l:%i%p on %b %e, %Y\'), c.body, c.status FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?'; And yes, you should be using DATETIME because there are so many native MySQL functions for comparing/manipulating DATETIME values. Okay, so my data-type is okay, but what about the issue of formatting in the Query versus formatting in PHP? Debbie Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 3, 2011 Share Posted September 3, 2011 I'd have to play around with it and see if anything special needs to be done to alias a field in a JOIN query; I don't use a lot of joins, and I don't remember. For the formatting, you can do either, really. It's usually faster to let MySQL handle it, but it may be easier to let php handle it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2011 Share Posted September 3, 2011 Use double quotes to define the string and then you don't need to escape the single quotes in the string. $q2 = "SELECT m.first_name, c.body, c.status, DATE_FORMAT(c.created_on, '%l:%i%p on %b %e, %Y') as date_created FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?"; I woudl suggest just trying the query. I don't think the 'on' in the date parameter will cause a problem because it is being treated as a strnig. But, if it does cause a problem, then you could just as easily get the two parts of the date separately. In fact, that is a better approach, since the "on" is really a separate "entity" from the date. If you ever wanted to make the application localized you would have some difficulty in doing so. $q2 = "SELECT m.first_name, c.body, c.status, DATE_FORMAT(c.created_on, '%l:%i%p') as time_created, DATE_FORMAT(c.created_on, '%b %e, %Y') as date_created FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?"; Quote Link to comment Share on other sites More sharing options...
doubledee Posted September 4, 2011 Author Share Posted September 4, 2011 Use double quotes to define the string and then you don't need to escape the single quotes in the string. $q2 = "SELECT m.first_name, c.body, c.status, DATE_FORMAT(c.created_on, '%l:%i%p on %b %e, %Y') as date_created FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?"; [/code] That seems to work okay. I would suggest just trying the query. I don't think the 'on' in the date parameter will cause a problem because it is being treated as a strnig. But, if it does cause a problem, then you could just as easily get the two parts of the date separately. In fact, that is a better approach, since the "on" is really a separate "entity" from the date. If you ever wanted to make the application localized you would have some difficulty in doing so. $q2 = "SELECT m.first_name, c.body, c.status, DATE_FORMAT(c.created_on, '%l:%i%p') as time_created, DATE_FORMAT(c.created_on, '%b %e, %Y') as date_created FROM member AS m INNER JOIN comment AS c ON m.id = c.member_id WHERE c.article_id=?"; I suppose that is why I prefer letting PHP format things. Thanks for the help and showing me how to do things two different ways. Debbie Quote Link to comment 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.