Re: [PHP] TimeStamp BEFORE 1970
On Jul 7, 2005, at 10:17 PM, Bruno B B Magalhães wrote: Hi Edward, thanks for replying! [snip] Why 1976, because it's a leap year and is between valid range for windows systems. Let me try to explain the rest. First I get the input year... which by the way is working fine here... preg_match('([0-2][0-9][0-9][0-9])', $input, $year); Was it clear, or I am dreaming awake? hehehehhe Actually I think I'm the one that is dreaming awake. ;) Operation + vicodin = fuzzy head. The math is amazing if it really works for you, and thanks for explaining what your doing. But my comments on the regex still stand. Just to be sure you understand that the match is working in this case because you are matching the whole string, which is what is in $year[0]. It would return the same results if you used the "normal" string delimiter, not sure that's the right term for it, of a forward slash instead of the parenthesis. ie preg_match('/[0-2][0-9][0-9][0-9]/', $input, $year); ^^ But in the end it's what works that counts, eh? ;) Edward Vermillion [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] TimeStamp BEFORE 1970 AND AFTER 2035
Just a quick fix, as now I've tested in a real environment, with a real application, and now it's working 100%, well, I think so. / * * Stritotime workaround for dates before 1970 and after 2038 */ function str2time($input = '01/01/1969') { if(($timestamp = strtotime($input)) !== -1 && $timestamp !== false) { return (float)$timestamp; } else { preg_match('([0-9][0-9][0-9][0-9])', $input, $year); $input = str_replace($year[0], '1976', $input); return (float)floor(strtotime($input) + (($year[0] - 1976) * (31557376.189582))); } } Shoot! Best Regards, Bruno B B Magalhaes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] TimeStamp BEFORE 1970
Hi Edward, thanks for replying! On Jul 7, 2005, at 10:44 PM, Edward Vermillion wrote: 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. Course it have, when we multiply the year offset by 31557376.189582 we are using the total seconds of an year, we normally don't use cause from 4 to 4 years we add a day (Pope Gregory XIII, Bregorian Calendar decreted in 1 March of 1582). So using the extra seconds we are, in theory adding that one more day. 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. Why 1976, because it's a leap year and is between valid range for windows systems. Let me try to explain the rest. First I get the input year... which by the way is working fine here... preg_match('([0-2][0-9][0-9][0-9])', $input, $year); Second I replace by a valid year between 1970 and 2025 preg_replace('([0-2][0-9][0-9][0-9])', '1976', $input); After calculate the date difference from 1976 to given date... let's say 01/01/1936. So we have... -40... and now multiply by number of seconds in a year (31557376.189582) before Gregorian Calendar, and we will get: -1262295047.583 Now we calculate the timestamp from 01/01/1976: 189313200 Now we have the final equation: 189313200 + (-1262295047.583). The result is a negative timestamp: -1072981847.583 Which if we put in a date function, we would get: 01/01/1936. Magic! We have negative timestamp in windows! Was it clear, or I am dreaming awake? hehehehhe Best Regards, Bruno B B Magalhaes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] TimeStamp BEFORE 1970
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 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] TimeStamp BEFORE 1970
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
Re: [PHP] TimeStamp BEFORE 1970
Hi Richard, Well, I took a look at, and I think it is TOO complex to handler a simple thing.. Well, that's my little contribution: function str2time($input = '12/31/1969') { $year= substr($input, -4); $input= substr_replace($input, 1976 , -4); return floor(strtotime($input) + (($year - 1976) * (31557376.189582))); } It work with ONLY a few hours more or less compared to my MacOS strtotime function, now I don't know which one is more accurate.. Well, why did you choose the year 1976, because it's an bisixth(?) year. So it was a matter of simple math. I would appreciate any help from everybody to: As I suppose that the last 4 digits are the year, I would like a pattern that could match a four digits number inside a string. Any body know how many seconds and microseconds have a year, I found a round number (31557376.189582). Well, any help is appreciate. At least now I can work on a windows box. Best Regards, Bruno B B Magalhaes On Jul 7, 2005, at 3:42 PM, Richard Davey wrote: Hello Bruno, Thursday, July 7, 2005, 7:04:44 PM, you wrote: BBBM> I've read the manual, and the ADOdb Date package functions, and BBBM> I am not using this because I want to keep my framework simple, BBBM> flexible, and fast. BBBM> Well, I just want a simple way to translate dates (I know what BBBM> is the input format) to unix timestamp, with ability to do this BBBM> with dates before 1970, and after 2023, is there any way? Personally I'd use the Pear Date package. It's stable, well formed and will do exactly what you require: http://pear.php.net/package/Date Even if you don't like the thought of using it - you can always pour over the source code to look at their methods and see how they handle it. Best regards, Richard Davey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] TimeStamp BEFORE 1970
Hello Bruno, Thursday, July 7, 2005, 7:04:44 PM, you wrote: BBBM> I've read the manual, and the ADOdb Date package functions, and BBBM> I am not using this because I want to keep my framework simple, BBBM> flexible, and fast. BBBM> Well, I just want a simple way to translate dates (I know what BBBM> is the input format) to unix timestamp, with ability to do this BBBM> with dates before 1970, and after 2023, is there any way? Personally I'd use the Pear Date package. It's stable, well formed and will do exactly what you require: http://pear.php.net/package/Date Even if you don't like the thought of using it - you can always pour over the source code to look at their methods and see how they handle it. Best regards, Richard Davey -- http://www.launchcode.co.uk - PHP Development Services "I do not fear computers. I fear the lack of them." - Isaac Asimov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] TimeStamp BEFORE 1970
Well, I've read the manual, and the ADOdb Date package functions, and I am not using this because I want to keep my framework simple, flexible, and fast. Well, I just want a simple way to translate dates (I know what is the input format) to unix timestamp, with ability to do this with dates before 1970, and after 2023, is there any way? My current system the PHP's microtime it's Ok, but one of ours test servers is running windows, and also some clients, so I rrealy need a overall solution. Here is my datetime class. / *** * @name Date and Time Class (./framework/libraries/datetime.class.php) * @version 1.0.0 * @dependencies None * @package B3M Platform™ 1.5.0 * @author B3M Development Team <[EMAIL PROTECTED]> * @copyright 2004 by Bruno B B Magalhaes * @copyright 2005 by B3M Development Team * @link http://www.bbbm.com.br/platform/ ***/ class datetime { / * * Get microtime */ function timestamp($input = null) { if(is_null($input)) { return (float)array_sum(explode(' ', (microtime() + datetime::server_timezone_offset(; } else { return (float)array_sum(explode(' ', strtotime($input))); } } / * * Format a microtime */ function format($timestamp = 0, $format = 'm/d/Y H:m:s') { return date($format, $timestamp); } / * * Get server's time-zone offset in seconds */ function server_timezone_offset() { if(!defined('_SERVER_TIMEZONE_OFFSET')) { return (float)date('Z'); } else { return (float)_SERVER_TIMEZONE_OFFSET; } } } ?> Thanks everybody! Best Regards, Bruno B B Magalhaes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php