I have a problem. The API dates are coming back to me like February 4, 2016. I need to apply some date modifications for which I need a date in the 02-04-2016 format. The code works well for dates returned by APIs that are above 9, like February 10, 2016, because when I manipulate this, I get it neatly like 02-10-2016. However, the problem is dates below 10, such as February 4, 2016, as they lead to 02-4-2016, which causes an error.
What I would like to know is how I can consistently get the 02-04-2016 format, regardless of the API dates, above 9 or below 10. Below is my code.
// Split checkin date string from API list($month, $day, $year, $time) = explode(' ', $checkindate); // change short month name (eg Feb) to month number (eg 02) $monthname = $month; $month = date('m', strtotime($monthname)); // new checkin date in example format 02-20-2016 $checkin_new = $month.'/'.$day.'/'.$year; // this is the part that causes an error when date returned by API is below 10, for example Feb 4 2016. Other dates above 9 such as Feb 10 2016 work well and don't cause an issue. // Subtract days $newdate = new DateTime($checkin_new ); $subtracteddate = $newdate->modify("-1 day");
source share