I have this DateTime cannot parse string error in php. i have tried applying solutions I read online but none has worked so far. My question is, is it possible for DateTime() to be disabled. Even the simplest code
$date = new DateTime('2010-5-5');
echo $date->format('Y-m-d);
Does not work.
I also tried using the static function createFromFormat(). And also I am not calling the DateTime class from a Class..so I do not need a back slash. What can I do please
Use this one for date format, So you can give the input format and get output format also
$date = DateTime::createFromFormat('d/m/Y', $date);
$fromdate = $date->format('Y-m-d');
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'm trying to format a date passed from a google plus Api thats like the guide says in RFC 3339 format:
PUBLISHED-> datetime-> The time at which this activity was initially published. Formatted as an RFC 3339 timestamp.
So by php documentation i found that:
DATE_RFC3339
Same as DATE_ATOM (since PHP 5.1.3)
And that both format are something like:
"Y-m-d\TH:i:sP"
Actually the output of the Google api is something like:
2014-01-22T10:36:00.222Z
When I'm trying to launch command like:
$date = DateTime::createFromFormat("Y-m-d\TH:i:sP", $activity['published']); //$activity['published'] contain the date
I have always FALSE as return.
In my opinion the problem is in the final part
.222Z
any suggestion will be appreciate before cutting it by some rudimental approach...
You don't need to use DateTime::createFromFormat() for standard inputs. Just use:
$date = new DateTime('2014-01-22T10:36:00.222Z');
var_dump($date);
But if you still insist to use createFromFormat(), then use correct format, with microseconds:
$date = DateTime::createFromFormat('Y-m-d\TH:i:s.uP', '2014-01-22T10:36:00.222Z');
var_dump($date);
There is a trick. A special constant DATE_RFC3339 was made to help, but it does not work if the last character is "Z" - which is perfectly fine for rfc3339 format. Actually JSON would specify format like that:
expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm
But using this DATE_RFC3339 you can receive an Error message from PHP:
InvalidArgumentException: The timezone could not be found in the database
That is why we need to specify format manually:
With DateTime
$date = DateTime::createFromFormat ('Y-m-d\TH:i:s.u\Z', $time);
With Carbon:
\Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.u\Z', $time);
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 was wondering if anyone could help me out with this.
I am running PHP Version 5.2.16.
Up until now I have used a substring.
$Date = substr($Date,0,10);
$Date = mysql_real_escape_string($Date);
I am scraping this string from an REST Api, so I have no control over its format, and I am not sure if the 'T' in the middle of the string could cause a problem.
Now this does the job simply enough but I was looking to use something more elegant like
$Date = Date::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
but this just returns the error:
Fatal error: Class 'Date' not found in...
Im quite new to php but I am assuming that I require the Date class (Common sense) but how would I implement this class into my script?
Any help or suggestions would be greatly appreciated.
Thanks :)
You could do:
<?php
$Date = strtotime("2011-10-31T16:22:00");
$converted = date("Y-m-d", $Date);
echo ($converted);
?>
Look here http://codepad.org/TNIwXDRp
The class is called DateTime
$Date = DateTime::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
http://php.net/book.datetime
However, because the format is standardized (ISO 8601) it should not surprisingly change, so it should be safe to just split it into date and time yourself
list ($date, $time) = explode('T', $string);
The class is not called Date but DateTime and the createFromFormat() method requires PHP/5.3 or greater. Whatever, the format you have is correctly recognised by the constructor so all you need is:
<?php
$date = new DateTime('2011-10-31T16:22:00');
echo $date->format('Y-m-d');
You can either use substr to extract, or simply use strtotime. I have used both and they are both useful solutions. strtotime would be better if you also would need to extract the timestamp.
I am retrieving the datetime data from mysql the retrieved data from a single row is.
2011-04-11 19:31:30
I wanted to reformat the datetime in d-m-Y H:i:s for that I am using date_format() the below code works just fine.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
However, I don't want to go object oriented way just for reformatting because I will be using the code within the foreach loop and that means I would have to initialize the DateTime class again and again.
I tried doing this the Procedural way using the following code.
$date = $users['registerDate'];
echo date_format($date, 'Y-m-d H:i:s');
the above code does not work for me and gives back the following error.
Warning: date_format() expects parameter 1 to be DateTime, string given in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/users/users.php on line 21
What could be possibly wrong?
The given solution works perfectly fine for the procedural way.
echo date('m-d-Y',strtotime($users['registerDate']));
However I would like to know which will be the best feasible solution the above procedural way or the OOP way.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
Considering I will be using the code within a foreach loop and it may loop for over a hundred times.
You do not need "date_format()":
echo date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
//results: 11-04-2011 19:31:30
You're trying to use a function/object that is part of the DateTime class without creating a reference to the DateTime class.
For procedural formatting take a look at date()
What you are seeking for is this:
date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
Have a look at the php-manual. However, using the methods you yourself proposed is pretty fine, since the DateTime-object maps to some functions written in C.
Also PHP Datetime is working properly, since date_format is just an alias of Date::format, which does exactly require what you don’t want to pass in (a DateTime-object).
Honestly, we’re talking about PHP...