I am facing a facing a problem with JHTMl calender method. Here is my code what I am trying to do:
edit.php
$this->event->from_date="2013-11-20 5:10:00";
$from_date = explode(" ", $this->event->from_date);
$from_time12 = date("m-d-Y", strtotime($from_date[0]));
echo JHTML::_('calendar', $from_time12, 'from_date', 'from_date', '%m-%d-%Y', array('class' => 'required', 'size' => '15', 'onchange' => 'getfromdate(this.value)'));
It's working fine for new item but when i edit the existing item it's shows error :
DateTime::__construct(): Failed to parse time string (10-22-2013) at position 0 (1): Unexpected character
I don't understand what's the problem. If anyone has suggestion please help.Thanks.
Understanding where is the problem
Generally problems are solved by understanding the error messages. In this case DateTime complains that it cannot parse the string 10-22-2013
, which is rather normal because this format is not accepted by DateTime
. When in doubt don't forget to check the manual . Investing a couple of hours in reading this stuff saves you in the future lots of time.
Fixing the code
For example, to get back to your error, try formatting the date as Ymd
inside $from_time12.
Improving the code
Also the conversions that you are doing there are rather confusing... Not only can strtotime
parse directly strings like 2013-11-20 5:10:00
, but also DateTime can also understand it. So you should be able to pass $this->event->from_date
directly to JHTML::_('calendar'...
) replacing $from_time12
instead of doing tons of conversions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.