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
Related
I am implementing the Bootstrap Date Range Picker on my Symfony CRM, in order for my client to choose a range of dates for a particular holiday season.
However, when it comes to adding the data to the database, it needs to be in the format of Start Date and End Date. The database expects a DateTime object, but I want it to be more readable on the front end for the client.
This is what gets passed back to the Controller once submitted:
20/5/2017 - 20/7/2017
In my Controller, I use the following code:
$dates = $form['startDate']->getData();
$date_bits = explode(" - ",$dates);
$startDate = \DateTime::createFromFormat('Y-m-d',$date_bits[0]);
$endDate = \DateTime::createFromFormat('Y-m-d',$date_bits[1]);
Except when I persist to the database I get this error:
Fatal error: Call to a member function format() on boolean in
/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php on line
53
So I tried this method instead:
$startDate = new \DateTime(str_replace("/","-",$date_bits[0]));
$endDate = new \DateTime(str_replace("/","-",$date_bits[1]));
$startDate = $startDate->format('Y-m-d');
$endDate = $endDate->format('Y-m-d');
But I receive the same error. What am I doing wrong here?
EDIT: Actually for the second code block, the error states I am calling format() on a string.
DateTime::createFromFormat() returning false means that whatever string representing the date you're submitting to it does not match the format you have provided.
The first parameter of DateTime::createFromFormat should be the format that you are submitting to it, in this case "d/m/Y". You can then do $startDate->format('Y-m-d') to get the date formatted the way you want it.
$dates = $form['startDate']->getData();
$date_bits = explode(" - ",$dates);
$startDate = \DateTime::createFromFormat('d/m/Y',$date_bits[0])->format('Y-m-d'); // 2017-05-20
$endDate = \DateTime::createFromFormat('d/m/Y',$date_bits[1])->format('Y-m-d'); // 2017-07-20
NB: Don't use->format() if you want it to remain as a DateTime object. Format always returns a string.
Usually with date I'm using this:
$startDate = new \Datetime($date_bits[0]);
$endDate = new \Datetime($date_bits[1]);
if you want to use CreateFromFormat try this please:
$startDate = new \DateTime::createFromFormat('d/m/Y', $date_bits[0])->format('Y-m-d');
$endDate = new \DateTime::createFromFormat('d/m/Y', $date_bits[1])->format('Y-m-d');
The error message:
Fatal error: Call to a member function format() on boolean in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php on line 53
is generated by code that attempts to call DateTime::format() on something that is not a DateTime object but a boolean (FALSE, to be more specific).
It is actually the value of $startDate, returned by DateTime::__construct() that doesn't know how to parse your input string.
You pass it a date like 20/5/2017. Obviously, its format is 'd/m/Y'.
DateTime::__construct() and strftime() do not understand it. They expect either m/d/Y or Y/m/d. It is impossible for them to tell apart d/m/Y from m/d/Y without any hint and the PHP programmers have chosen to ignore d/m/Y because m/d/Y is used more.
However, for date & time formats not understood directly, there always exists the function DateTime::createFromFormat(). It allows you to tell the engine what date & time components to search for into the string and in which order.
All you have to do is to use it properly:
$startDate = \DateTime::createFromFormat('d/m/Y',$date_bits[0]);
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
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));
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);
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.