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));
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 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 have been using the following code for a couple of months and it's been working fine, but now seems to have an issue with a certain time or time range.
I have a GPS tracker that sends it's date/time like this:
150102235335
The format is ymdhis
$input_array[6] = 150102235335;
$datetime = DateTime::createFromFormat('ymdhis', $input_array[6]);
$datetime = $datetime->format('Y-m-d h:i:s');
Using that time, php crashes with the following error:
PHP Fatal error: Call to a member function format() on a non-object
It seems that datetime ends up empty.
However, using the time 150103004933 works just fine.
Can anyone see where I have gone wrong here, or is this a bug?
Is there a better way to accomplish my date conversion?
I am using PHP 5.4.35
h is 12-hour format; and therefore 23 hours is invalid. Only values in the range 00-11 would be valid.
H is 24-hour format
Another simple question. I found this really cool snippet of code:
$date_str = "Jan 14th 2011";
$date = date_parse_from_format('M jS Y', $date_str);
echo $date->format('Y-m-d');
But when I run it on my computer it says Fatal error: Call to a member function format() on a non-object line 3.
The code was taken from here Converting date string to date so I presume it is good but it looks like it is referencing to an object that does not exist.
I have been looking at http://php.net/manual/en/function.date-parse-from-format.php and http://www.w3schools.com/php/php_ref_date.asp amongst many others but I have not found any any clues.
My question is should this code work as a standalone piece of code. If so why does it not work for me? Else, should I do to get it to work as expected.
date_parse_from_format();
returns associative array and you are trying to access the class method on a non object.
if you want to make use of PHP's inbuilt DateTime class. then more information here http://in2.php.net/manual/en/datetime.format.php
date_parse_from_format returns an array, not a DateTime object. What you want is
$date = date_create_from_format('M jS Y', $date_str);
^^^^^^---note the change
echo date('Y-m-d', $date);