Solved

Converting SQL server date to string

Posted on 2016-11-23
3
16 Views
Last Modified: 2016-11-23
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 108

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
 

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 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now