Solved

Converting SQL server date to string

Posted on 2016-11-23
3
72 Views
Last Modified: 2016-12-27
Hi I am creating a modify page in php. I am trying to extract the fields from the sqlserver table, display in php, then modify and save the changes. I have been able to get this to work in other forms, but this one contains a date data type in sqlserver. The error code I keep recieving is
"
Catchable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\PHPfiles\energymodifyform.php on line 121
This is confusing because in the sqlserver table the Date fields datatype is "date", not datetime.
I am posting below the php I used .
"
  <tr>
	<td><div align="center">
	<?php echo $result["DATE_REC"];?></div></td>

	<td><?php echo $result["D_06_GARE_D01"];?></td>
    <td><?php echo $result["D_05_GARE_D02"];?></td>
	
    <td><?php echo $result["D_12_GARE_D04"];?></td>
    <td><?php echo $result["D_08_GARE_D05"];?></td>
    <td align="right"><?php echo $result["D_10_GARE_D06"];?></td>
	
    <td align="right"><?php echo $result["USER_CREA"];?></td>
	
    <td align="right"><?php echo $result["DATE_CREA"];?></td>
   

	<td align="center"><a href="energymodify.php?ID=<?php echo $result["DATE_REC"];?>">Modify</a></td>	
  </tr>

Open in new window

0
Comment
Question by:peter Ojeda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41899473
Please go to line 121 in your script and use var_dump($result) to print out the variable, then post that here in the code snippet, thanks.

Also, please post the entire PHP script.  When the error is on line 121, but there are only 19 lines in the code snippet, we are surely missing something!
0
 
LVL 1

Author Comment

by:peter Ojeda
ID: 41899477
Hi ray sorry through about a hour and a half of trial and error I was able to get this to work. I used ->format("Y-m-d"). Went to close it 1 minute after you posted.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41899503
Yes, that's usually the best way to format a string from a DateTime object.

More examples here:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

Best of luck with your project, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question