Re: Another date exercise

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shawn McKenzie wrote:
> tedd wrote:
>> At 3:40 PM +0530 8/17/09, kranthi wrote:
>>> dont you think http://in3.php.net/strtotime is a solution to your
>>> problem ?
>> No, it's not a solution to my problem -- I have he problem solved.
>>
>> I was just asking if anyone wanted to submit their php solution. It was
>> only an exercise.
>>
>> I know there are numerous javascript solutions (some good, some bad),
>> but ALL of their data has to be accepted and scrubbed by a php script
>> anyway, so I was suggesting creating a php script to do it.
>>
>> If it's not a good exercise, then don't do it.
> 
> First stab at it.  Of course it needs US date ordering (month day year).
>  You can't do euro type dates or any other format because there is no
> way to tell the difference between 1/2/2009 (January) and 1/2/2009
> (February):
> 
> <?php
> 
> $dates = array(
> 'August 5, 2009',
> 'Aug 05 2009',
> 'Aug 5, 9',
> '08/05/09',
> '8-5-9',
> '8 05 2009',
> '8,5,9',);
> 
> foreach($dates as $date) {
>     $date = preg_replace("#([\d])+[^/\d]+([\d]+)[^/\d]+([\d]+)#",
> "$1/$2/$3", $date);
>     echo date("M j, Y", strtotime($date)) ."\n";
> }
> 
> ?>
> 

Guess you didn't like this one?  Also, if you change the / to - in the
preg_replace() it should work on Euro style dates.

$date = preg_replace("#([\d])+[^-\d]+([\d]+)[^-\d]+([\d]+)#",
"$1-$2-$3", $date);

-Shawn

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux