?
Solved

Converting SQL server date to string

Posted on 2016-11-23
3
Medium Priority
?
92 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
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
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 …
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 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