Incessant-Logic Posted October 19, 2005 Share Posted October 19, 2005 I'm in the process of creating a simple blog, partly as I want one and partly because I enjoy messing around. The blog is stored in a MySQL DB and each post has a timestamp field (column) blog_date I have used the basic DW connection and have inserted the timestamp field into my blog. This then displays as 20051019202020. How do I get that to display as 2005/10/19 20:20:20? I found an extension that will format a date, timestamp or mysql query(?) however I can't seem to work out how it finds the field. Also and on a slightly different topic, can you recommend a good site for free/cheap extensions? Quote Link to comment https://forums.phpfreaks.com/topic/2699-php-formatting/ Share on other sites More sharing options...
crispytown Posted October 26, 2005 Share Posted October 26, 2005 [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--] function builddatetime($date_time) { $year=substr($date_time,0,4); $month=substr($date_time,4,2); $day=substr($date_time,6,2); $hour=substr($date_time,8,2); $min=substr($date_time,10,2); $sec=substr($date_time,12,2); $str=date(\"Y/m/d H:i:s\", mktime($hour,$min,$sec,$month,$day,$year)); return $str; }[/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Try this function. It will convert date and time over to the format you wanted. An example of use.... [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php $post_date_time [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$row_data_grab[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'post_date_time\'[/span][span style=\"color:#007700\"]]; if (!empty([/span][span style=\"color:#0000BB\"]$post_date_time[/span][span style=\"color:#007700\"])){ [/span][span style=\"color:#0000BB\"]$post_date_time2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]builddatetime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$post_date_time[/span][span style=\"color:#007700\"]); echo [/span][span style=\"color:#0000BB\"]$post_date_time2[/span][span style=\"color:#007700\"]; } [/span][span style=\"color:#0000BB\"]?>[/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Quote Link to comment https://forums.phpfreaks.com/topic/2699-php-formatting/#findComment-9155 Share on other sites More sharing options...
Incessant-Logic Posted October 26, 2005 Author Share Posted October 26, 2005 Okay just tried that, I get an Undefined variable: row_data_grab my code in a test page is as follows: (all done in DW) [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]require_once([/span][span style=\"color:#DD0000\"]\'Connections/il_db.php\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]?> <?php mysql_select_db[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$database_il_db[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$il_db[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$query_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT blog_date, blog_title, blog_text, blog_author FROM blog WHERE blog_active = 1 ORDER BY blog_id DESC\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$query_blog[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$il_db[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]()); [/span][span style=\"color:#0000BB\"]$row_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalRows_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); function [/span][span style=\"color:#0000BB\"]builddatetime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"]) { [/span][span style=\"color:#0000BB\"]$year[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]4[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$month[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]4[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$day[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]6[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$hour[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]8[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$min[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]10[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$sec[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]12[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$str[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]date[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"Y/m/d H:i:s\"[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]mktime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$hour[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$min[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$sec[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$month[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$day[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$year[/span][span style=\"color:#007700\"])); return [/span][span style=\"color:#0000BB\"]$str[/span][span style=\"color:#007700\"]; } [/span][span style=\"color:#0000BB\"]?>[/span]<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /> <title>Untitled Document</title> </head> <body> [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]do { [/span][span style=\"color:#0000BB\"]?> [/span] <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_title\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_text\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_author\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php $post_date_time [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$row_data_grab[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'post_date_time\'[/span][span style=\"color:#007700\"]]; if (!empty([/span][span style=\"color:#0000BB\"]$post_date_time[/span][span style=\"color:#007700\"])){ [/span][span style=\"color:#0000BB\"]$post_date_time2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]builddatetime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$post_date_time[/span][span style=\"color:#007700\"]); echo [/span][span style=\"color:#0000BB\"]$post_date_time2[/span][span style=\"color:#007700\"]; } [/span][span style=\"color:#0000BB\"]?> [/span] </p> <hr width=\"80%\" align=\"center\" /> [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]} while ([/span][span style=\"color:#0000BB\"]$row_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"])); [/span][span style=\"color:#0000BB\"]?>[/span]</body> </html> [span style=\"color:#0000BB\"]<?php mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]?> [/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] Again any help appreciated. JC Quote Link to comment https://forums.phpfreaks.com/topic/2699-php-formatting/#findComment-9164 Share on other sites More sharing options...
crispytown Posted October 26, 2005 Share Posted October 26, 2005 I jsut grab a little bit for a script that i am working on... but here is your script modded with my function. [!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]require_once([/span][span style=\"color:#DD0000\"]\'Connections/il_db.php\'[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]?> <?php mysql_select_db[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$database_il_db[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$il_db[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$query_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#DD0000\"]\"SELECT blog_date, blog_title, blog_text, blog_author FROM blog WHERE blog_active = 1 ORDER BY blog_id DESC\"[/span][span style=\"color:#007700\"]; [/span][span style=\"color:#0000BB\"]$blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$query_blog[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$il_db[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]()); [/span][span style=\"color:#0000BB\"]$row_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$totalRows_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_num_rows[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); function [/span][span style=\"color:#0000BB\"]builddatetime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"]) { [/span][span style=\"color:#0000BB\"]$year[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]4[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$month[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]4[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$day[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]6[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$hour[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]8[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$min[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]10[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$sec[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]substr[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$date_time[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]12[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]2[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]$str[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]date[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"Y/m/d H:i:s\"[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]mktime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$hour[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$min[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$sec[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$month[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$day[/span][span style=\"color:#007700\"],[/span][span style=\"color:#0000BB\"]$year[/span][span style=\"color:#007700\"])); return [/span][span style=\"color:#0000BB\"]$str[/span][span style=\"color:#007700\"]; } [/span][span style=\"color:#0000BB\"]?>[/span]<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /> <title>Untitled Document</title> </head> <body> [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]do { [/span][span style=\"color:#0000BB\"]?> [/span] <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_title\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_text\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_author\'[/span][span style=\"color:#007700\"]]; [/span][span style=\"color:#0000BB\"]?>[/span]</p> <p>[span style=\"color:#0000BB\"]<?php $blog_date_time [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$row_blog[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'blog_date\'[/span][span style=\"color:#007700\"]]; if (!empty([/span][span style=\"color:#0000BB\"]$blog_date_time[/span][span style=\"color:#007700\"])){ [/span][span style=\"color:#0000BB\"]$blog_date_time2 [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]builddatetime[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog_date_time[/span][span style=\"color:#007700\"]); echo [/span][span style=\"color:#0000BB\"]$blog_date_time2[/span][span style=\"color:#007700\"]; } [/span][span style=\"color:#0000BB\"]?> [/span] </p> <hr width=\"80%\" align=\"center\" /> [span style=\"color:#0000BB\"]<?php [/span][span style=\"color:#007700\"]} while ([/span][span style=\"color:#0000BB\"]$row_blog [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"])); [/span][span style=\"color:#0000BB\"]?>[/span]</body> </html> [span style=\"color:#0000BB\"]<?php mysql_free_result[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$blog[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#0000BB\"]?>[/span] [/span][!--PHP-Foot--][/div][!--PHP-EFoot--] this variable "row_data_grab" is for my script. you needed to modify that for your mysql_fetch_assoc ... It is editted above so that should work for you now. Quote Link to comment https://forums.phpfreaks.com/topic/2699-php-formatting/#findComment-9165 Share on other sites More sharing options...
Incessant-Logic Posted October 27, 2005 Author Share Posted October 27, 2005 Great thanks worked. JC Quote Link to comment https://forums.phpfreaks.com/topic/2699-php-formatting/#findComment-9176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.