PDA

View Full Version : Stupid PHP Date Question


Wyatt
2006-12-21, 14:05
I have a problem with date() in a CMS I'm developing. When I output the date an article was created exactly as stored in my database, the date displays correctly. However, when I try to format it with date(), it doesn't work. It defaults back to December 31, 1969. Here's my code. If anybody can figure this out, I'd be thrilled.

This outputs December 31, 1969:
<?php echo date("F j, Y", $Posted);?>

This outputs the correct date without formatting:
<?php echo $Posted;?>

The output:
2006-12-18 15:46:54

I just checked my log, and I found this line:
A non well formed numeric value encountered in article.php on line 2

That's the line the date is output from. Am I formatting my dates wrong when they go in the database? Is that it?

Wyatt
2006-12-21, 16:18
Nevermind, I figured out a workaround. I hadn't considered using explode() to make an array and just recreate the timestamp in a valid format. It works fine now. :)

colivigan
2006-12-21, 16:34
The reason that your first method didn't work is that the PHP date function (http://us3.php.net/date) expects an integer timestamp, and you were passing it an already-formatted string returned from the database.

If your database column is a date type, there is probably an SQL function you could invoke in your select statement that would return it as integer seconds. But, since you already have a workaround, why bother? :)

Wyatt
2006-12-21, 16:43
The reason that your first method didn't work is that the PHP date function (http://us3.php.net/date) expects an integer timestamp, and you were passing it an already-formatted string returned from the database.

If your database column is a date type, there is probably an SQL function you could invoke in your select statement that would return it as integer seconds. But, since you already have a workaround, why bother? :)
Yeah, that's pretty much what it came down to. This is the first application I've developed in PHP. I'm used to ColdFusion's date handling, which handles formatted strings like a champ.

Overall, I'm much quicker with ColdFusion (that's what I use at work), since it's HTML-based and I have extensive HTML experience, but I'm adjusting to PHP and I'm really starting to like it.