943,740 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 17919
  • PHP RSS
Jun 20th, 2007
0

retrieve date from database to display on web page

Expand Post »
hi
I need to display date which is stored in database and need to format and display on web page the current format is (Y-m-d)
I need to display as (d-m-Y);
On the web page pls anybody help me with the php script I will paste my code:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $d=$_POST['sdd'];
  3. $m=$_POST['sdm'];
  4. $y=$_POST['sdy'];
  5. $string=date("Y/m/d",mktime(0,0,0,$m,$d,$y));
  6. $con=mysql_connect("localhost","root","");
  7. if(!$con)
  8. {
  9. die('UNABLE TO CONNECT TO DATABASE'.mysql_error());
  10. }
  11. mysql_select_db("dummy");
  12. //echo $string;
  13. mysql_query("insert into date1 (datevalue, count) values ('$string' , ' ')");
  14. $result=mysql_query("select date_format( datevalue , ' %d/ %m/ %Y ' ) as date from date1 order by count desc");
  15. if($row=mysql_fetch_array($result))
  16. {
  17. echo "Welcome user u selected the following date";
  18. echo "<br>";
  19. echo $row['datevalue'];
  20. echo "<br>";
  21. echo "Done displaying date";
  22. }
  23. mysql_close($con);
  24.  
  25. ?>
but it isnt displaying anything
Last edited by Ezzaral; Jan 30th, 2011 at 4:24 pm. Reason: Added code tags. Please use them to format all code that you post.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mithesh is offline Offline
13 posts
since Mar 2006
Jun 20th, 2007
0

Re: retrieve date from database to display on web page

Use the code below, edit all the red text with correct data.

<?php
$db_host = 'HOST';
$db_user = 'USER';
$db_pwd = 'PASS';

$database = 'DB';
$table = 'TABLE';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT 
  `TABLE`.FIELDNAME(DAY),`TABLE`.FIELDNAME(MONTH),`TABLE`.`FIELDNAME(YEAR)`
FROM
  `TABLE`
LIMIT 0 , 30");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<table border='0'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>

Save as PHP, if html phasing isn't enabled. Data should appear, In the correct format you wish.

If this information was helpful. Please add to my rep. Thanks
Last edited by Ezzaral; Jan 30th, 2011 at 4:23 pm. Reason: Added code tags.
Reputation Points: 46
Solved Threads: 48
Posting Pro in Training
macneato is offline Offline
410 posts
since Jun 2007
Apr 9th, 2009
0

Re: retrieve date from database to display on web page

Click to Expand / Collapse  Quote originally posted by mithesh ...
hi
I need to display date which is stored in database and need to format and display on web page the current format is (Y-m-d)
I need to display as (d-m-Y);
On the web page pls anybody help me with the php script I will paste my code:
<?php
$d=$_POST['sdd'];
$m=$_POST['sdm'];
$y=$_POST['sdy'];
$string=date("Y/m/d",mktime(0,0,0,$m,$d,$y));
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('UNABLE TO CONNECT TO DATABASE'.mysql_error());
}
mysql_select_db("dummy");
//echo $string;
mysql_query("insert into date1 (datevalue, count) values ('$string' , ' ')");
$result=mysql_query("select date_format( datevalue , ' %d/ %m/ %Y ' ) as date from date1 order by count desc");
if($row=mysql_fetch_array($result))
{
echo "Welcome user u selected the following date";
echo "<br>";
echo $row['datevalue'];
echo "<br>";
echo "Done displaying date";
}
mysql_close($con);

?>
but it isnt displaying anything

can you change you AS DATE i think DATE() is a reserved function in MYSQL you use DATE as an ALIAS.
Last edited by rm_daniweb; Apr 9th, 2009 at 6:13 am.
Reputation Points: 13
Solved Threads: 12
Junior Poster
rm_daniweb is offline Offline
165 posts
since Jan 2007
Jun 2nd, 2009
0

Re: retrieve date from database to display on web page

I just wanted to say, I was looking for an answer to this same question, and this post helped me a great deal.

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jdw1979 is offline Offline
2 posts
since Jun 2009
Jan 30th, 2011
0
Re: retrieve date from database to display on web page
PHP Syntax (Toggle Plain Text)
  1. $table=date,field name=date1,date2 , datatype=datetime
  2.  
  3. $sql="select date1 from date;
  4. $result=mysql_query($sql) or die(mysql_error());
  5. if($row=mysql_fetch_array($result))
  6. {
  7. $cr_date=date_create($row['date1']);
  8. $for_date=date_format($cr_date,'d-m-Y H:i:s');
  9. }
  10. print $for_date;
  11.  
Last edited by Ezzaral; Jan 30th, 2011 at 4:22 pm. Reason: Added code tags. Please use them to format all code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
antonyruban is offline Offline
2 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Register page - HELP
Next Thread in PHP Forum Timeline: header infor already set... In a line that does not exist!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC