Jump to content

strotime with dates prior to 1970


Steffen

Recommended Posts

Hi all

I've been searching for one hour now on the internet for an easy solution for strotime in combination with dates prior to 1970 (on Windows platform).
So, on my localhost strotime("1950-06-02") returns -1 instead of a timestamp.

I know there is a solution...I just can't find it:(

Grtz and thx
Link to comment
https://forums.phpfreaks.com/topic/6389-strotime-with-dates-prior-to-1970/
Share on other sites

[!--quoteo(post=360833:date=Apr 2 2006, 10:01 AM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 10:01 AM) [snapback]360833[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi all

I've been searching for one hour now on the internet for an easy solution for strotime in combination with dates prior to 1970 (on Windows platform).
So, on my localhost strotime("1950-06-02") returns -1 instead of a timestamp.

I know there is a solution...I just can't find it:(

Grtz and thx
[/quote]

the best workaround's i have found for dealing with this is in the manual: [a href=\"http://uk.php.net/strtotime\" target=\"_blank\"]http://uk.php.net/strtotime[/a] . its mostly the users comments you want to read as several of them discuss ways around pre-1970 dates.

hope it helps!
Cheers
[!--quoteo(post=360839:date=Apr 2 2006, 10:44 AM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 10:44 AM) [snapback]360839[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i searched there already...i didnt find it:(

thx!
[/quote]

there's a function that a user has written, on that page i gave you, that is a replacement for strtotime that deals with pre-1970 dates:

[code]
function safestrtotime ($s) {
   $basetime = 0;
   if (preg_match ("/(\d\d\d\d)/", $s, $m) && ($m[1] < 1970)) {
      if( $m[1] < 1902 ) {
         return -1;
      }
      $s = preg_replace ("/19\d\d/", $m[1]+68, $s);
      $basetime = 0x80000000 + 1570448;
  }
  $t = strtotime( $s );
  return $t == -1 ? -1 : $basetime + $t;
}
[/code]

do a search on the page for '1970' and you'll find several solutions. the above is just one of them
Yes, i tried one of those functions before but i had a problem with date(). so, now i have the same problem. One I try to convert the timestamp to a date it results in 19 january 2038 (= maximum value -> cfr php.net)

How can i solve the date function then?

Thx a lot!
[!--quoteo(post=360851:date=Apr 2 2006, 01:47 PM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 01:47 PM) [snapback]360851[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Yes, i tried one of those functions before but i had a problem with date(). so, now i have the same problem. One I try to convert the timestamp to a date it results in 19 january 2038 (= maximum value -> cfr php.net)

How can i solve the date function then?

Thx a lot!
[/quote]

well its safe to say that the function you've seen is pretty much the only way to convert one way. but because it's a custom function, my guess is that you'll have to write a custom function to convert it back, taking into account that negative values from the function off php.net is the amount of seconds BEFORE 1/1/1970. may take a little trial and error but shouldn't be too tricky, but date() only accepts a normal (positive) timestamp, not a custom-generated negative one.

I have a input form with year, month and day input fields. I convert it to a timestamp (with the new safestrotime function) to store it in the database (as VARCHAR). Then I select it and parse it with the date function...so it should return the date again...

thx a lot
[!--quoteo(post=360955:date=Apr 2 2006, 08:31 PM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 08:31 PM) [snapback]360955[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have a input form with year, month and day input fields. I convert it to a timestamp (with the new safestrotime function) to store it in the database (as VARCHAR). Then I select it and parse it with the date function...so it should return the date again...

thx a lot
[/quote]

do you HAVE to store it in the database as a timestamp (and why varchar anyway?) - why not just store it as a DATE type?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.