datetime - How to fix date inconsistency from external source PHP -


i have issue. dates api come me feb 4 2016. have apply date modifications need date in format 02-04-2016. code works dates returned api above 9, such feb 10 2016 because when manipulate this, neatly 02-10-2016. however, problem dates below 10, such feb 4 2016 because these lead 02-4-2016 causes error.

what know how can consistently format of 02-04-2016 regardless of dates api above 9 or below 10. following code.

// split checkin  date string api list($month, $day, $year, $time) = explode(' ', $checkindate);   // change short month name (e.g feb) month number (e.g. 02) $monthname = $month; $month = date('m', strtotime($monthname));  // new checkin date in example format 02-20-2016  $checkin_new = $month.'/'.$day.'/'.$year; // part causes error when date returned api below 10, example feb 4 2016. other dates above 9 such feb 10 2016 work , don't cause issue.   // subtract days  $newdate = new datetime($checkin_new );   $subtracteddate = $newdate->modify("-1 day"); 

for getting date in february use mktime function:

echo date("m d y ", mktime( 0,0,0,2, 4, 2016)); echo "<br />"; echo gmdate("m d y ", mktime( 0,0,0,2, 2, 2016)); 

this give output:

feb 04 2016 feb 01 2016 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -