toasty Posted October 18, 2006 Share Posted October 18, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/ Share on other sites More sharing options...
Destruction Posted October 18, 2006 Share Posted October 18, 2006 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 Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110777 Share on other sites More sharing options...
alpine Posted October 18, 2006 Share Posted October 18, 2006 [code]<?php$time = "2005-08-18 18:24:10"; // stored time$new_timeformat = date('h.i.s : d/m-Y',strtotime($time)); // transformecho $new_timeformat; // prints out: 06.24.10 : 18/08-2005?>[/code] Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110825 Share on other sites More sharing options...
Destruction Posted October 18, 2006 Share Posted October 18, 2006 aha I got the wrong function, thanks Alpine ;) lolDest Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110829 Share on other sites More sharing options...
toasty Posted October 18, 2006 Author Share Posted October 18, 2006 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 ) Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110859 Share on other sites More sharing options...
alpine Posted October 18, 2006 Share Posted October 18, 2006 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]<?phpfunction 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] Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110870 Share on other sites More sharing options...
toasty Posted October 18, 2006 Author Share Posted October 18, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/24352-how-do-you-change-a-variables-output-format/#findComment-110895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.