Jump to content

catchable fatal error : Object of class DateTime could not be converted to string.


Go to solution Solved by requinix,

Recommended Posts

I'm doing a conversion of a time between timezones.  The conversion part works fine, however when I go to use the $newtime variable as part of my sql statement I get the following error: catchable fatal error : Object of class DateTime could not be converted to string.

 

Here is my code ... any help is appreciated!:

// code to handle different timezone	
				   $time= $matchtime;
				   $sourceTimezone = new DateTimeZone('America/Halifax');
				   echo "Halifax :: " .$time;
				  
                                   $date = datetime::createFromFormat('H:i', $time, $sourceTimezone);
				   $destinationTimezone = new DateTimeZone('America/St_Johns');
				   $date->setTimezone($destinationTimezone);
				   $newtime=$date->format('H:i');
				  
                                    echo "<br>StJohns :: ".$newtime;		
							  		
		$str = "select * from tbl_schedule
			where B_id = '$id'
				and game_date = '$date' and team1_result <> 'Win'
				and team1_result <> 'Loss' and game_time <= '$newtime'";
  • Solution

"and game_date = '$date' and team1_result  'Win'"
$date is a DateTime object, not a string. You need to do the same kind of thing you did with $newtime: format() the time to get the Y-m-d value and use that in your query instead.

$newdate = $date->format('Y-m-d');
"and game_date = '$newdate' and team1_result  'Win'"
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.