Regular Expression string/ date issue php

Status
Not open for further replies.
Hi all,
Here is a problem I'm having. I have to import a text file with strings identify dates, but unfortunately they are in a weird format. Instead of using a leading zero to identify a day and month they use the following format:

112013 means January first 2013
10122013 means October 12th 2013

the format i need them in is:
01012013 January 1st 2013
10122013 means October 12th 2013

basically it needs to be a consistent 8 character date string with leading zeros for single digit values. I'm sure I can figure this out, but I'd like to do it with the least amount of code possible. Any suggestions?

Thanks,
Sam
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
. I'm sure I can figure this out, but I'd like to do it with the least amount of code possible. Any suggestions?

stop doing this. write it simply, and thus more likely correctly.

I you cannot see a terse way to do this now it is likely you will come back to this later and not recognise what you've done, and also significantly increase the risk of bugs.

not to mention either your example is wrong or you have no sufficiently clearly expplained the rules:

"1012013 means October 12th 2013"

either you meant

"1012013 means October 1st 2013" or
"10122013 means October 12th 2013"

alternatively:
There is very strange rules about truncation of years (unlikely)
The values of various months don't correspond to what we would expect (10

List the rules precisely before you start trying to do it 'shorter'
 

MirrorImage

Seniorius Lurkius
7
dlp":2kcilgkg said:
Also, please tell me what date corresponds to "1112013".

Your choices are January 11th, 2013 or November 1st, 2013. The question is how do you figure out which one is right?

You can logic out every date except for "111" through "119" and "121" through "129". That's either Jan11-Jan19 or Nov1-Nov9, and Jan21-Jan29 or Dec1-Dec9. Those have to be solved within the context of the system (what does the date refer to compared to today's date), but you're still looking at a mess of If/Then/Else statements for everything else.

You can solve February through October based on the first 2 digits, and most of January, November, and December based on whether it's a 6 or 8 digit date, plus any 30th and 31st (no "Month 13") and any 10th and 20th based on leading zeroes.
 

hanser

Ars Legatus Legionis
43,847
Subscriptor++
XspackerX":2mmpwa2j said:
Hi all,
Here is a problem I'm having. I have to import a text file with strings identify dates, but unfortunately they are in a weird format. Instead of using a leading zero to identify a day and month they use the following format:

112013 means January first 2013
1012013 means October 12th 2013

the format i need them in is:
01012013 January 1st 2013
10012013 means October 12th 2013

basically it needs to be a consistent 8 character date string with leading zeros for single digit values. I'm sure I can figure this out, but I'd like to do it with the least amount of code possible. Any suggestions?

Thanks,
Sam
You've settled on an implementation without specifying what the underlying problem is.

1) What's the real problem you're trying to solve? You're transforming date strings from one form to another... why? Do you have control over the output you're consuming? Do you have control over the acceptable input that your consuming system is using?

2) Use DateTime objects with DateTime::createFromFormat and/or strtotime. This'll let you extend the system or change output format pretty easily in the future.

Of course, most everything in #2 is useless if your date formatting rules are inconsistent.
 
Status
Not open for further replies.