How to fix date inconsistency from an external PHP source

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"); 
+5
source share
6 answers

To get the date in February, use the 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 will produce the result:

 Feb 04 2016 Feb 01 2016 
+1
source

mYou can just ask PHP to do all the work right away:

 $formatted_date = date('md-Y', strtotime($chekindate)); 
0
source

Try using the strtotime function strtotime :

 echo date('md-Y', strtotime('Feb 4 2016')); 
0
source

I managed to find a solution. It may not be perfect, but it works.

Basically the problem was that I needed to add 0 earlier, for example 4, when the day is below 10. Days above 9 already work well. So I wrote a simple if statement and combined it in code. Updated parts are shown 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 (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.'/'.**$patchday**.$day.'/'.$year; // Subtract days $newdate = new DateTime($checkin_new ); $subtracteddate = $newdate->modify("-1 day"); 
0
source

use a DateTime object that can accept many formats

 echo (new DateTime('Feb 4 2016'))->format('md-Y'); 
0
source

You should use the DateTime::createFromFormat with the accepted month without leading zeros, a three-month month, and spaces between them, using F j Y as follows:

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

and then format it, for example:

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

If time is also returned from the API, you just need to add it to the createFromFormat line in the correct format (which you can find on the documentation page), for example, H:i:s ;

 $date = DateTime::createFromFormat('F j YH:i:s', $checkindate); 
0
source

Source: https://habr.com/ru/post/1240779/


All Articles