I suddenly get the error message Fatal error: Call to a member function setTimezone() on a non-object after adding some new lines of code:
first of all, i used DateTime to switch between different timeformats to calculate DST, etc.
I used the following code [Code Block 1]
foreach ($result as $k=>$v)
{$timestamp=DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $k,new DateTimeZone("UTC")); //take current date from $k as new DateTile Object
$timestamp->setTimezone(new DateTimeZone('Europe/Berlin')); //convert timezone
$date[]=$timestamp->format('Y-m-d H:i:s'); // "export" the date to date-array
}
This code works well, until now:
I added 20 code lines above an other DateTime function [Code Block 2]:
$ts=new DateTime("#".round($_GET['startdate']/1000));
$startdate=$ts->format('Y-m-d\TH:i:s.u\Z');
After I added these 2 lines, all the time i get the error: Fatal error: Call to a member function setTimezone()for this line:$timestamp->setTimezone(new DateTimeZone('Europe/Berlin'));
...but i can't see an relation / connection between these to code parts?!
What's happened here?
Thank you, for any idea.
//Update: as you can see at the comments, i'm using PHP 5.3.6
again: the code block 1 works, if i remove the code block 2
Looks like not all of array keys of $result is valid date strings. The DateTime class returns a new DateTime instance or FALSE on failure. And when $k is not valid, $timestamp is false, which is not an object.
Related
I simply just want to convert the date in PHP. I am using PHP 7.0.33. It was working fine before but suddenly it started producing a Fatal error
Below is my code.
When I use format() I get
Fatal error: Uncaught Error: Call to a member function format() on boolean
$date = DateTime::createFromFormat('d/m/Y', $dates[$i]);
$date = $date->format("Y-m-d");
Any help will be great!!
DateTime::createFromFormat() returns FALSE in case of error, so the problem is in this function call. Check the value of $dates[$i].
I can see that there are many questions along these lines but I am very confused as to why the following does not work, taken straight from the PHP docs:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate;
The error:
PHP Catchable fatal error: Object of class DateTime could not be converted to string.
In fact every example in the docs gives me this error. Any ideas?
You can't echo the DateTime object directly. You have to use the format method to get the date and / or time part:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate->format('Y-m-d H:i:s');
// NOT echo $tempDate!!!
demo: http://ideone.com/IyqRWj
If you want to see the details of the object (for debug) you can use var_dump:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
var_dump($tempDate);
The error message:
PHP Catchable fatal error: Object of class DateTime could not be converted to string.
is self-explanatory. The statement:
echo $tempDate;
attempts to print a DateTime object. The echo() language construct expects a string, you pass it a DateTime object. The DateTime class does not implement the __toString() magic method and PHP doesn't know how to convert a DateTime object to string.
There are so many ways to represent a DateTime object as string and all of them are handled by the DateTime::format() method.
In fact every example in the docs gives me this error.
In fact, every example in the documentation of DateTime::createFromFormat() reads:
echo $date->format('Y-m-d');
which is a different thing that echo $date;.
Any ideas?
Read the documentation of DateTime and DateTime::format() carefully.
I'm having some troubles using the format function in Symfony2 when trying to insert a date interval into a table, for the purpose of setting due dates for created invoices.
Here is what I have:
$today = new \DateTime();
$interval = $today->add(new \DateInterval('P1M'));
$invoice->setDueDate($interval->format('Y-m-d H:i:s'));
However, when I hover over the format parameter in PHPStorm, it tells me that it's expecting a DateTime object not a string, and I get the following error in my profiler:
Error: Call to a member function format() on string
So, I changed the line to this:
$invoice->setDueDate($interval->format(new \DateTime()));
But when I run that, my profiler gives this error:
Warning: DateTime::format() expects parameter 1 to be string, object given
It almost seems like a catch 22 situation! I am really baffled, do I use a string or a DateTime object, because either one fails yet warns me I need to use one or the other..
Any ideas?
I’d guess that $invoice->setDueDate() is the one expecting a DateTime instance. So the line should be $invoice->setDueDate($interval);
I ask here well knowing that there is other questions and answers on this and i have been going through them:
Calculate number of hours between 2 dates in PHP
But i still can't find a solution to my problem.
Here is the scenario:
Im using the Google Calendar Api. I have a foreach() loop running, where it graps the dateTime() for the event.
I want to get the differens from the start and end time. My code is as follows:
$neweventDateStr = new DateTime($event->start->dateTime);
$neweventDateEnd = new DateTime($event->end->dateTime);
$diff = $eventDateEnd->diff($neweventDateStr);
However i get the following error on the last line:
Fatal error: Call to a member function diff() on string
I have checked and both variables are recognised as dateTime Objects.
Therefore i can't figure out why is giving me the error as listed above.
Any clue as to why this is?
Note, the code:
$event->start->dateTime
Returns the value of the date and time from the google server as a dateTime RFC 3339 format: source:
https://developers.google.com/google-apps/calendar/v3/reference/events
It returns the values like so:
$event->start->dateTime = 2015-07-06T14:30:00+02:00
$event->se->dateTime = 2015-07-06T15:30:00+02:00
I have tried to:
echo $neweventDateStr;
and i returns the error(as expected) that the variable couldn't be turned into a string since its a dateTime Object format.
The problem is in your code:-
$diff = $eventDateEnd->diff($neweventDateStr);
it should be :-
$diff = $neweventDateEnd->diff($neweventDateStr); // because no where $eventDateEnd is defined.
An example is as follows:-
<?php
$neweventDateStr = new DateTime('2015-04-04 10:10:10');
$neweventDateEnd = new DateTime('2015-04-10 10:10:10');
$diff = $neweventDateEnd->diff($neweventDateStr); // instead of $eventDateEnd i write $neweventDateEnd
print_r($diff);
?>
Output:-https://eval.in/393320
I am trying to format 2014-03-27 00:53:31 to be: 03/27/2014 I have tried many solutions but none of which have worked. My most recent is explained in this question.
So the time is stored in database as a datetime like: 2014-03-27 00:53:31
I call this by $customer->last_login;
Then I am trying to format this by doing the following:
$dt = $customer->last_login;
echo $dt->format('m/d/Y');
When I run this I get the following error:
Fatal error: Call to a member function format() on a non-object
What am I doing wrong? Or what is a better solution to formatting this to display just the date?
If $customer->last_login is a string, you have to convert to a DateTime object first.
$dt = new DateTime($customer->last_login);
echo $dt->format('m/d/Y');
Try this
echo date('m/d/Y',strtotime($dt));