问题描述:

I have an issue. The dates from the API come back to me as Feb 4 2016. I have to apply some date modifications for which I need the date in format 02-04-2016. The code works well for the dates returned from the API that are above 9, such as Feb 10 2016 because when I manipulate this, I get it neatly as 02-10-2016. However, the problem is with the dates below 10, such as Feb 4 2016 because these lead to 02-4-2016 which causes an error.

What I would like to know is how I can consistently get the format of 02-04-2016 regardless of dates from API are above 9 or below 10. The following is my code.

// Split checkin date string from API

list($month, $day, $year, $time) = explode(' ', $checkindate);

// change short month name (e.g Feb) to 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; // 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");

网友答案:

For getting the 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 would give output:

Feb 04 2016
Feb 01 2016
网友答案:

mYou can simply ask PHP to do all the job at once:

$formatted_date = date('m-d-Y', strtotime($chekindate));
网友答案:

Try using the function strtotime directly:

echo date('m-d-Y', strtotime('Feb 4 2016'));
网友答案:

I managed to think of a solution. May not be ideal, but it works.

Basically, the problem was that I needed an added 0 before for example 4 when the day is below 10. Days above 9 work well already. So I wrote a simple if statement and concatenated it in the code. Updated parts are in bold

// Split checkin  date string from API
list($month, $day, $year, $time) = explode(' ', $checkindate); 

// if day is below 9, give var patchday value 0. Otherwise leave it empty
**if ($day < 10) { $patchday = "0"; } else { $patchday = ""; };**

// change short month name (e.g Feb) to month number (e.g. 02)
$monthname = $month;
$month = date('m', strtotime($monthname));

// new checkin date in example format 02-20-2016
 $checkin_new = $month.'/'.**$patchday**.$day.'/'.$year;

// Subtract days
 $newdate = new DateTime($checkin_new ); 
 $subtracteddate = $newdate->modify("-1 day");
网友答案:

use DateTime object which can accept many formats

echo (new DateTime('Feb 4 2016'))->format('m-d-Y');
网友答案:

You should use the DateTime::createFromFormat method with the month accepted without leading zeros, a three letter month, and spaces in between, by using F j Y like this;

$date = DateTime::createFromFormat('F j Y', $checkindate);

and then format it, like this;

$formatteddate = $date->format('m-d-Y');

If the time is also returned from the API, then you just need to add it to your createFromFormat string in the correct format (which you can find on the docs page) such as H:i:s;

$date = DateTime::createFromFormat('F j Y H:i:s', $checkindate);
相关阅读:
Top