Jump to content

Archived

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

toasty

How do you change a variables output format?

Recommended Posts

I'm not sure if what I'm trying to do is possible, or if it's as easy as I'm hoping it is.  I've got a variable [color=green]$time[/color] which has a value format in the database of [color=red]Y-m-d H:i:s[/color].  When the variable is called it has an output of the exact same format.  What I'm trying to do is retain the format of the value in the Database but change how that value's displayed on the output.  It seems like it should be really easy but for the life of my I haven't yet been able to figure it out.  ???


[size=7pt]Question's related to [url=http://www.phpfreaks.com/forums/index.php/topic,111728.msg452913.html]this post[/url][/size]

Share this post


Link to post
Share on other sites
I believe there is a way to do this in the mysql query using DATE_FORMAT.  You may wish to look this up.  I'll post it if I find the syntax needed.  Also, I believe you can use mktime() to change it to a unix timestamp and date() to reformat. 

Hope this helps set you on the right path,

Dest

Share this post


Link to post
Share on other sites
[code]

<?php

$time = "2005-08-18 18:24:10"; // stored time
$new_timeformat = date('h.i.s : d/m-Y',strtotime($time)); // transform
echo $new_timeformat;  // prints out: 06.24.10 : 18/08-2005

?>

[/code]

Share this post


Link to post
Share on other sites
Hey wow, thanks guys!  Okay, let me see if I can complicate this scenario any further then.  The output of the $time is sent to a .tpl file via this snippet of code
[code]function formatNewsPost($poster,$email,$avatar,$title,$post,$time,$comicID,$nl2br) {
$post = $this->nl2brNewsPost($post,$nl2br);
if (!empty($avatar))
$avatar = '<img src="' . $avatar . '" alt="' . $poster . '" border="0" />';

$replace['{NEWS_POSTER}'] = $poster;
$replace['{NEWS_EMAIL}'] = $email;
$replace['{NEWS_AVATAR}'] = $avatar;
$replace['{NEWS_TITLE}'] = $title;
$replace['{NEWS_POST}'] = $post;
$replace['{NEWS_DATE}'] = $time;
$replace['{COMIC_ID}'] = $comicID;

return $this->_common->getTemplate('news_print.tpl', $replace);
}[/code]

Would I need to insert/replace the [color=red]$time[/color] variable with the [color=red]$new_timeformat[/color]?

Also, with [color=blue]$time = "2005-08-18 18:24:10"; // stored time[/color], what value do I need to use?  When the news_print.tpl file calls the $time variable it's printed out with whatever timestamp is associated with the specific post. Ex: the news_print.tpl might send six seperate outputs to a single page, each having different values (as per the code above) including different timestamps.

Thanks for your help guys, you've no idea how much this helps (actually, you probably do  :D )

Share this post


Link to post
Share on other sites
If your present $time variable IS like you described, it is currently sendt to the function formatNewsPost()
You could convert it before sending it to the function or do it inside, like this:

[code]

<?php

function formatNewsPost($poster,$email,$avatar,$title,$post,$time,$comicID,$nl2br) {
$post = $this->nl2brNewsPost($post,$nl2br);
if (!empty($avatar))
$avatar = '<img src="' . $avatar . '" alt="' . $poster . '" border="0" />';
        $time = date('h.i.s : d/m-Y',strtotime($time)); // <-- re define $time
$replace['{NEWS_POSTER}'] = $poster;
$replace['{NEWS_EMAIL}'] = $email;
$replace['{NEWS_AVATAR}'] = $avatar;
$replace['{NEWS_TITLE}'] = $title;
$replace['{NEWS_POST}'] = $post;
$replace['{NEWS_DATE}'] = $time; // <-- and here it goes to the template
$replace['{COMIC_ID}'] = $comicID;

return $this->_common->getTemplate('news_print.tpl', $replace);
}

?>

[/code]

Share this post


Link to post
Share on other sites
[color=blue][size=15pt]Yes![/size][/color]

That was exactly the bit of code I needed (as far as I can tell).  Sorting is still coming out the way it's supposed to be according to the Datetime field and it now looks exactly as I need it to.  Woo hoo!!  Alpine, Destruction, thank you guys soo freakin' much for your help.  Totally saved my ass.  ;D

Share this post


Link to post
Share on other sites

×

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.