For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2007 > PHP4 date parsing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP4 date parsing
Preben

2007-02-25, 6:58 pm

Hi everyone


How do I parse a date with PHP version 4.

The problem is that the date-format is like:

d-m-Y ( 15-08-2006 )


but strtotime won't accept this even though the setlocale function has
been used:
setlocale( LC_ALL, "da_DK", "da", "dan", "danish", "da_DK.ISO8859-1" );


Thanks for helping!

/ Preben
Preben

2007-02-25, 6:58 pm

> How do I parse a date with PHP version 4.
>
> The problem is that the date-format is like:
>
> d-m-Y ( 15-08-2006 )
>
>
> but strtotime won't accept this even though the setlocale function has
> been used:
> setlocale( LC_ALL, "da_DK", "da", "dan", "danish", "da_DK.ISO8859-1" );


I tried to list all the locales on the server - and there are none.. So
that's probably why.

So now I just used the split-function to parse the date manually.
Rik

2007-02-26, 3:59 am

On Sun, 25 Feb 2007 20:42:56 +0100, Preben <64bitNONOSPAMno@mailme.dk>
wrote:

>
> I tried to list all the locales on the server - and there are none.. So
> that's probably why.
>
> So now I just used the split-function to parse the date manually.


Thank you for this wonderfull example against using strtotime(). Never
liked the unpredictability of this function :-)
--
Rik Wasmus
Michael Fesser

2007-02-26, 6:59 pm

..oO(Rik)

>Thank you for this wonderfull example against using strtotime(). Never
>liked the unpredictability of this function :-)


It's all documented:

| The function expects to be given a string containing a US English date
| format [...]

http://www.php.net/manual/en/function.strtotime.php

| For numeric months, the ISO 8601 format `year-month-day' is allowed
| [...] The construct `month/day/year', popular in the United States, is
| accepted. Also `month/day', omitting the year.

http://www.gnu.org/software/tar/man...111.html#SEC111

There's no word about locales, so you can't expect strtotime() to
recognize every localized date format.

In my projects I use my own date parser class, which uses some regular
expressions to recognize the most common formats (especially the German
dd.mm.yy[yy], which is what I usually need). If the matching fails, I
call strtotime() just as a fallback.

Micha
Preben

2007-02-26, 6:59 pm

Michael Fesser skrev:
> .oO(Rik)
>
>
> It's all documented:
>
> | The function expects to be given a string containing a US English date
> | format [...]
>
> http://www.php.net/manual/en/function.strtotime.php


Well, the documentation for setlocale states that it contains date
formatting settings - but looking closer it seems it's only for strftime
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com