DateTime::__construct(): Failed to parse time string

Martooti

Member
After I add custom calc element :
Code:
date_default_timezone_set('Africa/Cairo');
$start_date='{fb_events___start_date_raw}';
$timestamp = strtotime($start_date);
$date = new DateTime($start_date);
$now = new DateTime();
 
$between = $date->diff($now)->format("%d");
$divide=intval($between/7);
$days =$divide*7;
$days2 =$days.'days';
$newdate = date_add($date,date_interval_create_from_date_string($days2));
$category ='{fb_events___category_raw}';
 
if ($category=='weekly events') {return date_format($newdate, 'Y-m-d H:i');}
      elseif (date('Y-m-d H:i', $timestamp)=='0000-00-00 00:00') {return date('Y-m-d H:i', $timestamp);}
            else {return date_format($now, 'Y-m-d H:i');};

I am getting an error " DateTime::__construct(): Failed to parse time string ({fb_events___start_date_raw}) at position 0 ({): Unexpected character " every time I try to add a new data to the list.

I have element fb_events___start_date set to

Time format: H:i
Date format: Y-m-d (both list and form)
Show time selector :no
Any help appreciated.
Online: http://redsea.abdoo-webdesign.com/events
 
Looks like your {fb_events___start_date_raw} placeholder doesn't exist, so it winds up getting passed as a literal string to the DateTime constructor. Is this error happening when you load the page? If so, what do you have the "only calc on save" set to on your calc element? if that is set to No, then we will run the calc every time it is displayed, which means on a new form, we'd be running it when rendering the form, at which point start_date wouldn't have a value.

So make sure "only calc on save" is set to Yes. That way, we should only run the calc on submission, at which point start_date should have a value.

If it still blows up, put some defensive coding in there, to test that the $start_date has actually been assigned a valid date string.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top