Jump to content

display stored date differently


jacko_162

Recommended Posts

Use the mysql DATE_FORMAT() function in your query to return a DATE value formatted any way you want - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

 

i had a read through and came up with the following code;

 

echo date_format($date, 'Y-m-d');

 

i get a

Fatal error: Call to undefined function: date_format()

 

i assume i dont get this in my version of PHP... any work arounds?

Link to comment
Share on other sites

There's the DATE_FORMAT function in MySQL that called via the query, then there's the date function in PHP used in conjunction with the strtotime function. You're confusing the two.

 

If you want to use the PHP function, do something like:

<?php
echo date('F j, Y',strtotime($date));
?>

assuming that the $date variable holds the value from your database.

 

Ken

Link to comment
Share on other sites

Because it's a MySQL function, not a PHP function. In the DB query: SELECT DATE_FORMAT(field_name, '%b %e, %Y') AS date FROM tbl_name . . . should do it. Then your array element with the date will be along the lines of $array['date'].

Link to comment
Share on other sites

There's the DATE_FORMAT function in MySQL that called via the query, then there's the date function in PHP used in conjunction with the strtotime function. You're confusing the two.

 

If you want to use the PHP function, do something like:

<?php
echo date('F j, Y',strtotime($date));
?>

assuming that the $date variable holds the value from your database.

 

Ken

 

worked a treat, ty :)

Link to comment
Share on other sites

Hopefully my last question today in relation to this whole time/date thing.

 

 

i have the following php code;

 

<?php
function imp($char,$tag){
foreach($char as $key=>$value){
$char[$key] = $value;
}
$char = implode($tag,$char);
return $char;
} ?>


<?php
$sql = "SELECT * FROM `tests` WHERE date <= '$todaydate' AND date >= '$oldDate' AND member_id='1' ORDER BY ID ASC LIMIT 0,10";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
$test1[] = $row[test1];
$test2[] = $row[test2];
$test3[] = $row[test3];
$test4[] = $row[test4];
$test5[] = $row[test5];
$test6[] = $row[test6];
$test7[] = $row[test7];
$test8[] = $row[test8];
$test9[] = $row[test9];
$test10[] = $row[test10];
$test11[] = $row[test11];
$test12[] = $row[test12];
$test13[] = $row[test13];
$test14[] = $row[test14];
$date[] = $row[date];
}}
    

?>


<?php echo imp($test14,",")?>

 

the $test1 variable is then added like so:

1,2,3,4,5,6,7,8,9,10

 

if i do the same to date i get;

2010-05-09,2010-05-09,2010-05-09 etc....

 

how can i format the date with the above ideas to show like this;

06.05.10 (basically dd.mm.yy)

 

appreciate the help and the learning curves :)

 

Link to comment
Share on other sites

thank you for reply,

 

yes that bit i get, when i echo it out i get "09.05.10" which is perfect.

 

but the above code needs to list out the entire row of results for example i want it to echo the following;

 

11.04.10,06.05.10,07.05.10 etc..

 

so seperating each result with a ","

 

the coding in my above post ouputs the date like this;

 

"2010-04-11,2010-05-06,2010-05-07,2010-05-0"

 

if i use;

<?=imp($date,",")?>

 

how and where can i modify the above coding to ouput the date like this?

Link to comment
Share on other sites

Not a major at PHP, but couldn't you just use a for statement for format each one before you use the imp() function?

 

Like so:

for ($i = 0; $i <= count($date); $i++) {
  $date[$i] = date('d.m.y', strtotime($date[$i]));
}

 

Pretty sure this would work, and then you'd use your:

imp($date,",")

 

Only thing is that i'm not aware of any more-efficient methods of doing this, so if someone posts a better one then i'll learn something too!

 

But yeah, that loop should do what you're seeking?

Link to comment
Share on other sites

^ That's a more efficient way, then. I also thank you ken.

 

And i'm sorry, I just wasn't sure whether you could format arrays :) I know a lot more Java, so I tend to go about doing it how I would in Java, incase I mess anything up - and in Java you wouldn't be able to do that!

 

Please post if Ken's code works! :)

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.