Re: TimeStamp BEFORE 1970

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

 




On Jul 7, 2005, at 7:54 PM, Bruno B B Magalhães wrote:

Hi Folks,

Well I think I got it, at least it's working more or less to me now...
I really would appreciate any comments or suggestions.

    function str2time($input = '12/31/1969')
    {
        if(($output = strtotime($input)) !== -1)
        {
            return $output;
        }
        else
        {
            preg_match('([0-2][0-9][0-9][0-9])', $input, $year);
            preg_replace('([0-2][0-9][0-9][0-9])', '1976', $input);
return floor(strtotime($input) + (($year[0] - 1976) * (31557376.189582)));
        }
    }

Thanks a lot!

Best Regards,
Bruno B B Magalhaes

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



One problem is that there's no accounting for leap years, but I don't know if that's gonna cause you any problems or not.

The other thing I noticed is that the 'match' bit you have in there as "year[0]" should be "$year[1]". $year[0] will return the whole string that was matched, not just the actual match part between the parenthesis. Although I think you would get the same thing with your set up. And you will need to put a delimiter in the regex part, right now it looks like it's going to treat the parenthesis as the delimiter which will make the return for the match not work. ie:

" preg_match('([0-2][0-9][0-9][0-9])', $input, $year); " -- $year will be empty...

should be " preg_match('/([0-2][0-9][0-9][0-9])/', $input, $year); "

or " preg_match('/([0-1][0-9][0-7][0-9])/', $input, $year); " -- to restrict it to 1970 or before....

but it could also be " preg_match('/([\d]{4})/', $input, $year); " -- if you don't really need to validate the the year

There's other problems that I can see with the math logic in the return, like why 1976?, why would you want to generate a positive number that will conflict with dates before 1970? but it could just be that I'm not thinking the math all the way through, and what you eventually want to do with the dates once you store them.

Edward Vermillion
evermillion@xxxxxxxxxxxx

--
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