Jump to content


Photo

PHP formatting


  • Please log in to reply
4 replies to this topic

#1 Incessant-Logic

Incessant-Logic
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 19 October 2005 - 05:19 PM

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?

#2 crispytown

crispytown
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 26 October 2005 - 12:17 PM

[!--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--]

#3 Incessant-Logic

Incessant-Logic
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 26 October 2005 - 04:29 PM

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

#4 crispytown

crispytown
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 26 October 2005 - 06:29 PM

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.



#5 Incessant-Logic

Incessant-Logic
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 October 2005 - 09:18 AM

Great thanks worked.

JC




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users