Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a little regex help on dates 3

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
0
0
GB
I have a bit of regex code....

Code:
preg_match('#(?P<month>\w+) (?P<day>\d+) (?P<year>\d+)#', $dataAB[0], $haveDate0);

for dates that are formatted as for example, Aug 14, 2010, it will output...

$haveDate0[year]
$haveDate0[month]
$haveDate0[day]

but if the date is formatted as Aug 4, 2010... the day not being a two digit number... not starting with a zero... then the regex does't recognize anything.

is there a simple way to update my regex so it recognizes both 1 and 2 digit days?
 
Hi

pushyr said:
for dates that are formatted as for example, Aug 14[COLOR=red yellow],[/color] 2010, it will output...
[gray](...)[/gray]
but if the date is formatted as Aug 4[COLOR=red yellow],[/color] 2010...
Are you sure the problem is the number of digits ? I would say, it is the comma ( , ) after the day, which is not included in your regular expression.

By the way, it works for me.


Feherke.
 
Hi

Forgot the suggestion : include an optionally comma, so will work with and without it :
Code:
[COLOR=darkgoldenrod]
preg_match[/color][teal]([/teal][green][i]'#(?P<month>\w+) (?P<day>\d+)[highlight],?[/highlight] (?P<year>\d+)#'[/i][/green][teal],[/teal] [navy]$dataAB[/navy][teal][[/teal][purple]0[/purple][teal]],[/teal] [navy]$haveDate0[/navy][teal]);[/teal]

Feherke.
 
Why fight with a regular expression which are klunky and slow, when there are already built in expressions for these tasks. strtotime() and date() will help you ou:

Code:
$mydate="Aug 4, 2010";

$myD=strtotime($mydate);
$haveDate0['year']=date("Y", $myD);
$haveDate0['month']=date("m", $myD);
$haveDate0['day']=date("d", $myD);

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
ok, what i failed to mention was that

the date format that is not being recognized is formatted as...

Aug 4, 2010 ....and not Aug 4, 2010

there are two spaces before the 4
 
ok, sorted it using this...

Code:
#(?P<month>\w+) \s?(?P<day>\d+),? (?P<year>\d+)#

cheers for your help guys!
 
this should work for normal variations
Code:
$pattern ='/(?P<month>Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sept|September|Oct|October|Nov|November|Dec|December)\s*(?P<day>\d{1,2})(?:nd|st|rd|th)?\s*\,?\s*(?P<year>\d{4})/ms';

but if you the date is in its own string (i.e. you are not searching for the date in a larger subset of code) then vacunita's comment is on the money.

Code:
list($year, $month, $day) = explode('-', date('Y-m-d', strtotime($dateString)));
 
ok, cool jpadie... the date is in it's own string... so will be using the lighter code as suggested by yourself and vacunita
 
if you want the short month and single digit day in the array substitute
Code:
'Y-M-j'
for
Code:
'Y-m-d'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top