php createFromFormat fails with datepicker -
i have below datepicker in form:
$( '.datepicker' ).datepicker({ dateformat: 'dd-mm-yy' });
then in php code processes form have:
$campaigndate = datetime::createfromformat('d-m-y', $formdata['campaign']['campaign_date'] ); if ($campaigndate) { $campaignunixdate = $campaigndate->gettimestamp(); } else { $response = array( 'status' => 'failed', 'message' => 'failed create campaign date', 'redirect' => '' ); echo json_encode($response); return; }
$campaigndate seems false, assume have wrong format createfrom cant work out issue.
solution
use uppercase y
instead of lowercase y
$campaigndate = datetime::createfromformat( 'd-m-y', $formdata['campaign']['campaign_date'] );
explanation
the date formats don't match
$( '.datepicker' ).datepicker({ dateformat: 'dd-mm-yy' });
would return
18-07-2015
while
$campaigndate = datetime::createfromformat( 'd-m-y', $formdata['campaign']['campaign_date'] );
uses format
18-07-15
from php docs:
y
full numeric representation of year, 4 digits
examples: 1999 or 2003y
2 digit representation of year (which assumed in range 1970-2069, inclusive) examples: 99 or 03 (which interpreted 1999 , 2003, respectively)
Comments
Post a Comment