Solved

Converting SQL server date to string

Posted on 2016-11-23
3
51 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 109

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 109

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

821 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