-2

I had a external XML file url and with that I need to import data from that XML data. Unfortunately in xml data schedule date value is incorrect like: 20082020 but it should be 20/08/2020 or any proper date value.

I don't have access to chance that XML, So, all I need to do retrieve proper date format from "20082020" to '20/08/2020' or '20-08-2020' in PHP

mlimon
  • 661
  • 8
  • 19
  • nope, as you can see php strtotime or any other function that suppose to convert string to date format is not working with this string value. – mlimon Aug 28 '20 at 07:39
  • I already tried :) – mlimon Aug 28 '20 at 07:39
  • 1
    _“nope, as you can see php strtotime […]”_ - and as _you_ can see, there is more than one answer posted under that question … With `DateTime::createFromFormat`, this is trivial. – CBroe Aug 28 '20 at 07:43
  • @CBroe I did see bro, but maybe you didn't see what is the value I need to convert. anyway I tried but failed maybe I using incorrect way. – mlimon Aug 28 '20 at 07:54
  • 3
    _“maybe I using incorrect way”_ – that is why it is important that you _show us_ what you tried, instead of going “nope” or “didn’t work”. `DateTime::createFromFormat('dmY', '20082020')` parse this into a DateTime instance, which you can then use to output any date format you like again. – CBroe Aug 28 '20 at 07:56
  • Can you share what you've tried so far? If you say that you failed to get it working, it could help to see your attempts – Nico Haase Aug 28 '20 at 07:57
  • 1
    I would defiently go with @CBroe s answer/comment - cleanest answer you find here so far. – Stender Aug 28 '20 at 08:02
  • @CBroe perfect man, thanks didn't thought about removing space in first parameter lol. – mlimon Aug 28 '20 at 11:12

2 Answers2

-1

If the date inside this XML-file is always in this format (DDMMYYYY), you can just re-format it like so:

$col_date = '20082020';
$real_date = date_parse( preg_replace('|(\d{2})(\d{2})(\d{4})|', '\1-\2-\3', $col_date) );
Damocles
  • 1,287
  • 1
  • 7
  • 9
  • 1
    ah I see, didn't know that actually don't have any previous experience with XML, thanks for the info ans solution as well :) – mlimon Aug 28 '20 at 07:45
  • Why do you use regex for this if you could solve it with simple string function as [substr](https://www.php.net/manual/en/function.substr.php) – DarkBee Aug 28 '20 at 07:46
  • 1
    @DarkBee Do you have any valid reasons against using regex here? – Damocles Aug 28 '20 at 07:53
  • @DarkBee I will disagree with your point `substr()` is not perfect for this job. – mlimon Aug 28 '20 at 07:54
  • Using regex is to expensive for cases like this. See [here](https://stackoverflow.com/questions/6433492/preg-match-vs-strpos-for-match-finding) – DarkBee Aug 28 '20 at 07:55
  • @DarkBee can you care to share an example for this solution with `substr()` thanks – mlimon Aug 28 '20 at 07:56
  • Deleted my substr answer. It's still available [here](https://ideone.com/2KcCvQ). Comment from @CBroe is best [answer](https://stackoverflow.com/questions/63629390/retrieve-date-from-plain-string-in-php/63629534?noredirect=1#comment112517766_63629390) – DarkBee Aug 28 '20 at 08:06
  • @DarkBee Yes, one `substr` call compared to one regex call is considerably faster. However, you need 3 `substr` calls to accomplish this. – Damocles Aug 28 '20 at 08:08
  • @Damocles [demo](https://www.darkbee.be/stack/regex.php) – DarkBee Aug 28 '20 at 08:18
  • @DarkBee - can you include the `DateTime::` method in that demo, to check the comparison on that as well? just interested. – Stender Aug 28 '20 at 08:21
  • never mind - applied it here : https://www.tehplayground.com/UAkXsmKHTQOD7Mhz – Stender Aug 28 '20 at 08:31
-2

I'm still curious to know that there is any built-in PHP function exists to convert

20082020 => 20-08-2020

But I need to move my project forward, So I did with simple regex. So, anyone need here is it.

$date = '20082020';
echo preg_replace('/^(\d{2})(\d{2})(\d{4})$/', '$1-$2-$3', $date);

I'm not sure this is the best way to fix this one, So, anyone have better idea feel free to share.

mlimon
  • 661
  • 8
  • 19