Jump to content

Date output error


doubledee

Recommended Posts

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

 

Link to comment
Share on other sites

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() ] )

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?  :shrug:

 

 

Debbie

 

Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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=?";

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.