PHP code with six digits in human readable format

I have a set of dates that are formatted like this ...

197402 192201 184707 

The first four digits represent the year, and the other two represent the month. I try to output them in this format

 February 1974 January 1922 July 1847 

I tried passing it a date function like this ...

 echo date ('F Y', 197402) 

But that gives me January 1970 every time, so I guess I misunderstood how the date function works, can anyone help?

+5
source share
5 answers

I would use the DateTime class , you can create from a specific format and then output to another.

As indicated in the comments below, you also need to set the day to the first month, otherwise you will get unwanted results if the current day is more than the number of days in this month.

 echo DateTime::createFromFormat('Ymd', 19470201)->format('F Y'); 
+4
source

You get β€œJanuary 1970” as the output because you tried to create a date from the timestamp 197402 , which is in seconds since January 1, 1970. If you output the complete line from it (with seconds and whatnot), you will see that it is a valid timestamp giving the actual date, but they all end in early January 1970, see this online demo .

This format, YYYYMM, is not a recognizable format for most functions. You need to split it if you know that the format will be that way - and use this data instead. You can use substr() to split the string, and then convert the numeric month to the string associated with that month using date() and mktime() (since you just specify the year and month).

Next snippet

 $arr = [197402, 192201, 184707]; foreach ($arr as $v) { $year = substr($v, 0, 4); $month = substr($v, 4, 2); echo date("FY", mktime(0, 0, 0, $month, 0, $year))."<br />"; // mktime() produces a valid timestamp based on just month and year // Alternatively, drop mktime() and use strtotime() and create from a standard format, // while specifying a date in the month (which won't matter to the output) // echo date("FY", strtotime("$month/01/$year"))."<br />"; } 

displays

February 1974
January 1922
July 1847


Alternatively, you can use the DateTime class (which is much easier to work with) and create from a given format using date_create_from_format()

 foreach ($arr as $v) { echo date_create_from_format('Yh', $v)->format('F Y')."<br />"; } 

This will generate the same output as above.

References

+5
source

When you pass an int for today, it treats it as a Unix timestamp. To create a date object from a predefined format, use DateTime::createFromFormat .

echo DateTime::createFromFormat('Ym',198403)->format('F Y');

leads to March 1984

+3
source

You need to add a day to it, for example. just add β€œ01” and then use strtotime to convert this to a unix timestamp, as the date () function expects a timestamp as a parameter.

eg.

 echo date('F Y', strtotime("19220101")); 
+2
source

You have a text representation of dates in a non-standard format. You must first analyze them and convert them to timestamps (the number of seconds from January 1, 1970, 00:00:00 UTC). The PHP date() function can only work with timestamps.

The best approach (as of 2017) is to use the DateTime PHP class to handle date and time:

 foreach (array('197402', '192201', '184707') as $text) { $date = DateTime::createFromFormat('Ym', $text); echo($date->format('F Y')."\n"); } 

The DateTime::createFromFormat() method parses the string using the specified format and creates a new DateTime object if the parsing succeeds. This is the equivalent of OOP strtotime() , but smarter (because it can get hints about what date components should look for in the input string.)

The DateTime::format() method creates a textual representation of the date using the provided format. This is the equivalent of OOP date() .

The OOP ( DateTime* classes ) approach is recommended (and better than the procedural approach) because it has built-in support for time intervals (the date and time procedural functions do not have this.)

+1
source

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


All Articles