Jump to content


Photo

strotime with dates prior to 1970


  • Please log in to reply
10 replies to this topic

#1 Steffen

Steffen
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 02 April 2006 - 09:01 AM

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

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 09:39 AM

[!--quoteo(post=360833:date=Apr 2 2006, 10:01 AM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 10:01 AM) View Post[/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

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Steffen

Steffen
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 02 April 2006 - 09:44 AM

i searched there already...i didnt find it:(

thx!

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 09:52 AM

[!--quoteo(post=360839:date=Apr 2 2006, 10:44 AM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 10:44 AM) View Post[/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:

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

do a search on the page for '1970' and you'll find several solutions. the above is just one of them
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Steffen

Steffen
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 02 April 2006 - 12:47 PM

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!


#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 02:20 PM

[!--quoteo(post=360851:date=Apr 2 2006, 01:47 PM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 01:47 PM) View Post[/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.


"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 02 April 2006 - 07:12 PM

What are you trying to do? There may be another solution that doesn't require strtotime().
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Steffen

Steffen
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 02 April 2006 - 07:31 PM

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

#9 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 07:48 PM

[!--quoteo(post=360955:date=Apr 2 2006, 08:31 PM:name=Steffen)--][div class=\'quotetop\']QUOTE(Steffen @ Apr 2 2006, 08:31 PM) View Post[/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?
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 02 April 2006 - 07:50 PM

A MySql DATE type column can hold dates between '1000-01-01' and '9999-12-31'.

You can't use unix date functions on anything before 1970 but MySql DATE_FORMAT() etc should work.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 Steffen

Steffen
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 03 April 2006 - 08:03 AM

ok, i will try that...but i never worked with it....but i'll find out^^!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users