?
Solved

Converting SQL server date to string

Posted on 2016-11-23
3
Medium Priority
?
79 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 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

Industry Leaders: 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 shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

752 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