Jump to content

[SOLVED] php Date


tekrscom

Recommended Posts

I feel very retarded right now because I'm asking one of those questions that I asked several years ago... all over again... but anyway...

 

So... I am trying to print a date from a MySQL 5.0.81 database in a DateTime format using PHP 5.2.5...

 

When I echo the raw date, it is correct, but when I attempt any notion of using date(), forget it, nothing works...

 

I was originally trying this...

date('D M d, Y h:i a', $row['ThreadDatePosted'])

 

But during testing I simplified it down to just Y-m-d...

    $ThreadDatePosted = $row['ThreadDatePosted'];
    echo $ThreadDatePosted;
    //$ThreadDatePosted = date('D M d, Y h:i a', $row['ThreadDatePosted']);
    $ThreadDatePosted = date('Y-m-d', $ThreadDatePosted);

 

Thinking that I was going mad, I started pouring over articles again about using php w/mysql datetime, but this is still what I was coming up with... Can someone please enlighten me?

Link to comment
https://forums.phpfreaks.com/topic/173854-solved-php-date/
Share on other sites

Have you echoed out $row['...']?

 

 

Chances are, it's a string representation of the DATETIME field, and the date() function takes an integer representing a Unix timestamp as its second argument.

 

 

That means at some point you need to convert the text to a unix timestamp.

 

You can either use strtotime(), or (and this is the way I suggest), you could use UNIX_TIMESTAMP(ThreadDatePosted) as uThreadDatePosted

Link to comment
https://forums.phpfreaks.com/topic/173854-solved-php-date/#findComment-916455
Share on other sites

The second parameter in the date() function is a Unix Timestamp -

Description

string date ( string $format [, int $timestamp ] )

timestamp

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given.

 

If that is not what you are putting in it, the code you are using won't work will it?

 

If your database column is a DATE or DATETIME data type, why not just use the mysql DATE_FORMAT() function directly in your select query to format it any way you want - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

 

Link to comment
https://forums.phpfreaks.com/topic/173854-solved-php-date/#findComment-916456
Share on other sites

Have you echoed out $row['...']?

 

 

Chances are, it's a string representation of the DATETIME field, and the date() function takes an integer representing a Unix timestamp as its second argument.

 

 

That means at some point you need to convert the text to a unix timestamp.

 

You can either use strtotime(), or (and this is the way I suggest), you could use UNIX_TIMESTAMP(ThreadDatePosted) as uThreadDatePosted

 

Yes, I did try to just echo out the $row['...']... Same result...

 

Could you give a small example of how UNIX_TIMESTAMP(ThreadDatePosted) as uThreadDatePosted would work... I tried this... but it didn't work...

$ThreadDatePosted = date('Y-m-d', UNIX_TIMESTAMP($ThreadDatePosted));

Link to comment
https://forums.phpfreaks.com/topic/173854-solved-php-date/#findComment-916459
Share on other sites

Ok, got it

 

$query = "SELECT ForumThreads.*, UNIX_TIMESTAMP(ForumThreads.ThreadDatePosted) as uThreadDatePosted, Users.Username FROM ForumThreads LEFT JOIN Users ON ForumThreads.PosterUserID = Users.UserID WHERE TopicID = '$_GET[TopicID]'"; 

 

Yeah!!! Thanks guys

Link to comment
https://forums.phpfreaks.com/topic/173854-solved-php-date/#findComment-916463
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.