Jump to content

[SOLVED] DATE_FORMATing Help


Taorluath

Recommended Posts

So here's the deal:

 

I have a blog database that I made myself. The post date is included as a field in my database. It's stored in the usual YYY-MM-DD format. I want it to be displayed easier to read, like: Jan 2nd, 2008. I'm trying to get DATE_FORMAT() to make it look better, but I can't seem to get it to work.

 

Here's my code:

$sql = "SELECT id, poster, DATE_FORMAT(post_date, '%b %D, %Y'), post_name, post_text, current_location FROM blog_posts ORDER BY post_date";
$res = mysqli_query($mysqli, $sql);

post_date is the field that has the date stored in it, btw.

WHat's going wrong?

Link to comment
Share on other sites

  • 2 weeks later...

Better to use a column alias

 

SELECT id, poster, DATE_FORMAT(post_date, '%b %D, %Y') as displayDate, ...

 

then

 

echo $row['displayDate'];

 

I think all of this is awesome and I've been trying to get something...ANYTHING to work..lol and I gotta tell you I'm about at the end of my rope..

 

PLEASE tell me..

Date_Format($registerDate, ''%b %D, %Y') as displayDate

echo $row['displayDate'];

 

for some reason that or anything else i try to do is working it just gives me errors and/or displays dates like this -> 2008-05-05 20:23:32

I cannot get it to work ... Question

would this work-

$registerDate="Date_Format($registerDate, ''%b %D, %Y') as displayDate"

echo $row['displayDate'];

...

 

Thank you!!!

Link to comment
Share on other sites

@cowboydude

 

SELECT id, poster, DATE_FORMAT(the_name_of_your_post_date_field, '%b %D, %Y') as displayDate, ... 

 

Notice there's no dollar symbol in the name of the posst date field (whatever it's called)

Link to comment
Share on other sites

Question

would this work-

$registerDate="Date_Format($registerDate, ''%b %D, %Y') as displayDate"

echo $row['displayDate'];

...

 

Thank you!!!

 

No.

 

DATE_FORMAT() is a MySQL function and is only going to work inside a MySQL query.

 

If you want to format a date in PHP use date() function (www.php.net/date)

Link to comment
Share on other sites

Question

would this work-

$registerDate="Date_Format($registerDate, ''%b %D, %Y') as displayDate"

echo $row['displayDate'];

...

 

Thank you!!!

 

No.

 

DATE_FORMAT() is a MySQL function and is only going to work inside a MySQL query.

 

If you want to format a date in PHP use date() function (www.php.net/date)

 

This is why I'm having a great deal of trouble...

 

I need to format the date from 2008-05-05 23:23:12 to 05/05/2008

 

I have tried the $query format but it will not return correctly...

 

Can you give me a hint or clue here...

 

Here is what I'm using for output in php

<b>User Since:<font color="red"> <?php echo $registerDate;?></b></font><br>

 

I can get the $registerDate info but for the life of me cannot figure out how to write the correct mysql query to format it..believe me I've been trying for 3 days and looking at everything I can find to read and still I just cannot get this..

 

 

 

 

Link to comment
Share on other sites

METHOD A

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%d/%m/%Y') as registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo $row['registerDate'], '<br />';
}
?>

 

METHOD B

<?php 
$sql = "SELECT registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo date('d/m/Y', strtotime($row['registerDate'])), '<br />';
}

?>

 

Link to comment
Share on other sites

METHOD A

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%d/%m/%Y') as registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo $row['registerDate'], '<br />';
}
?>

 

METHOD B

<?php 
$sql = "SELECT registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo date('d/m/Y', strtotime($row['registerDate'])), '<br />';
}

?>

 

 

Well BINGO... that worked.. now I have to 'tweak' to just show the record for the user logging in.. right now it's showing me all the registration dates for all my users..lol

 

 

Link to comment
Share on other sites

METHOD A

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%d/%m/%Y') as registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo $row['registerDate'], '<br />';
}
?>

 

METHOD B

<?php 
$sql = "SELECT registerDate FROM tablename";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
echo date('d/m/Y', strtotime($row['registerDate'])), '<br />';
}

?>

 

 

Well BINGO... that worked.. now I have to 'tweak' to just show the record for the user logging in.. right now it's showing me all the registration dates for all my users..lol

 

NOW.. how about this?

 

$sql = "SELECT DATE_FORMAT(registerDate, '%m/%d/%Y') as registerDate FROM jos_users";

$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res))

{

echo $row['registerDate'], '<br />';

}

 

adding this to the end of $sql - WHERE user=$usr_id";

 

I am able to capture the userid now but so I can show ONLY the record for the user logging in... I'm gonna lose my mind here.. learning is VERY hard..lol

 

The date is formatting correctly Thanks to the MAJOR help with the above script but it is grabbing all the registerdates in the database and printing them out...

 

I'm think using the $USER_ID would help me isolate only the user who is logged in...

 

am I on the right track here?

 

Link to comment
Share on other sites

if $usr_id contains the id you want to query then, yes...

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%m/%d/%Y') as registerDate 
          FROM jos_users
          WHERE user = '$usr_id' ";

 

EDIT If $usr_id is a number then the single quotes are optional

Link to comment
Share on other sites

if $usr_id contains the id you want to query then, yes...

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%m/%d/%Y') as registerDate 
          FROM jos_users
          WHERE user = '$usr_id' ";

 

EDIT If $usr_id is a number then the single quotes are optional

 

Ok now I think I'm looking at this all wrong... your script did exactly what it was told to do.. get $registerDate and it does.. now when I try to narrow it down by adding the $usr_id it gives me an error

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

I tried it both with quotes and without quotes... I think I'm making this WAY to hard and that's why I can't figure it out...

 

I need to get the user..then get his/her register date format it and show it.  The correct format for user is $user in the database...

 

WOW...

 

 

Link to comment
Share on other sites

if $usr_id contains the id you want to query then, yes...

<?php
$sql = "SELECT DATE_FORMAT(registerDate, '%m/%d/%Y') as registerDate 
          FROM jos_users
          WHERE user = '$usr_id' ";

 

EDIT If $usr_id is a number then the single quotes are optional

 

Ok now I think I'm looking at this all wrong... your script did exactly what it was told to do.. get $registerDate and it does.. now when I try to narrow it down by adding the $usr_id it gives me an error

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

I tried it both with quotes and without quotes... I think I'm making this WAY to hard and that's why I can't figure it out...

 

I need to get the user..then get his/her register date format it and show it.  The correct format for user is $user in the database...

 

WOW...

 

 

I'm sorry here is the entire package...

 

<?php

$user =& JFactory::getUser();

$usr_id = $user->get('id');

echo $usr_id;

$sql = "SELECT DATE_FORMAT(registerDate, '%m/%d/%Y') as registerDate

          FROM jos_users

          WHERE user = $usr_id";

$res = mysql_query($sql);

while ($row = mysql_fetch_assoc($res))

{

echo $row['registerDate'], '<br />';

}

?>

Link to comment
Share on other sites

Find out what the error is. Use

$res = mysql_query($sql) or die(mysql_error());

 

$sqlc = 'SELECT `data` FROM `jos_session`';

$resc = mysql_query($sqlc) or die(mysql_error());

while ($row = mysql_fetch_assoc($resc))

{

echo $row['data'], '<br />';

}

 

I am trying to do this all wrong I believe.. there's a table called jos_session and it stores the info for the logged in user...

 

I ran the above but used 'data' and I got this in return:

 

s:2:"id";s:2:"62";s:4:"name";s:11:"Cowboysdude";s:8:"username";s:11:"cowboysdude";s:5:"email";s:23:"hidden@hidden;s:12:"registerDate";s:19:"2008-05-05 20:28:22";

 

I believe the numbers are id numbers in that field... That's the info I'm after..

 

Many Thanks!!!

 

 

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.