Codingphp - stroftime returns -1

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  ALL
32290.1 
I'm helping someone with some php code and I've found something I can't figure out:

PHP code:
echo odbc_result($rs,'date_of_birth');
$dob = strtotime(odbc_result($rs,'date_of_birth'));
echo $dob;


returns "1953-03-30 00:00:00" and "-1" for the two echos.

And yet for another record it works fine, returning "1980-10-14 00:00:00" and "340326000".

I can't see any difference between the dates in the database (which is an Access one; yes I know)

Any ideas?


AH! I think I just figured it out. *test* Yup, it's because the date is before the 1-Jan-1970 epoch.

Ok... so I can't believe this is the first time anyone has used birth dates with PHP, so any suggestions on how to fix it?
0/0
 Reply   Quote More 

 From:  Matt  
 To:  Peter (BOUGHTONP)     
32290.2 In reply to 32290.1 
I'm pretty certain that's a limitation of the Win32 builds of PHP, it not being able to calculate a timestamp from a date that falls before the Unix epoch.

To fix it you'd ideally adjust the query to return the date column in a format better supported by PHP. If you can get it to return it as a unix timestamp then that would be ideal, but I guess you can't do that otherwise you already would have, right?

In which case try using mktime() instead, and exploding the result from the database, like so:

php code:
 
    list($date, $time) = explode(' ', odbc_result($rs, 'date_of_birth'));
 
    list($year, $month, $day) = explode('-', $date);
    list($hour, $minute, $second) = explode(':', $time);
 
    echo mktime($hour, $minute, $second, $month, $day, $year);

doohicky

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
32290.3 In reply to 32290.1 

You need to update PHP I reckon. Unless it's a platform issue, which you using access suggests it may be.

 

If the dates are in a regular format in the database it's probably going to be easiest to write your own function to convert them to timestamps. Why the fuck are they in the db as strings in the first place? -_-


0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Matt     
32290.4 In reply to 32290.2 
t'll teach me to post after not refreshing for a while.

0/0
 Reply   Quote More 

 From:  Matt  
 To:  Drew (X3N0PH0N)     
32290.5 In reply to 32290.4 
You'd think so.

doohicky

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Matt     
32290.6 In reply to 32290.2 
Don't know how to change the query to return it in a different format. :$

That code you posted looks good - far better than all the stupid "safestrtotime" suggestions I've come across.

Thanks.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Drew (X3N0PH0N)     
32290.7 In reply to 32290.3 
Can't (easily) update PHP version - it needs to all be self-contained, and is using phpDev for that.

They are stored as date/time values in the db, just not returning as a proper timestamp for whatever reason... I'll blame Microsoft. :)
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Matt     
32290.8 In reply to 32290.2 
Bum, that code doesn't work either. :(

Probably just make it a known limitation / "feature" of the system - only people younger than 37 can use it.
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Peter (BOUGHTONP)     
32290.9 In reply to 32290.8 

Where does it fail? there's nowt in that that should fail except perhaps mktime. But if that's it then that's some fucking bullshit and you should complain.

 

But anyway, surely there's a query you can use to get the date out as you want it if it's stored as a date object thing?


0/0
 Reply   Quote More 

 From:  Matt  
 To:  Peter (BOUGHTONP)     
32290.10 In reply to 32290.8 

Is there a specific reason why you can't upgrade PHP? I've just tested the code on PHP 5 here on Windows and it works fine.

 

If it's the easy PHP installation you're after, it's not all that hard to create your own set up routine for Apache and PHP.

doohicky

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Drew (X3N0PH0N)     
32290.11 In reply to 32290.9 
quote:
But if that's it then that's some fucking bullshit and you should complain.

It is and I did for a bit, but I think they got a bit miffednonplussed at me.


quote:
surely there's a query you can use to get the date out as you want it if it's stored as a date object thing?

Possibly. It was just a SELECT * though.


quote: Matt
Is there a specific reason why you can't upgrade PHP? I've just tested the code on PHP 5 here on Windows and it works fine.

This is for a project which will be demoed, so they're using phpDev as a portable server type thingy, and that doesn't have a version with PHP 5.


quote:
If it's the easy PHP installation you're after, it's not all that hard to create your own set up routine for Apache and PHP.

Because it's a project that would have to be justified and explained and stuff - easier for them to say "I used this ready-made tool thing" or
whatever (especially since the deadline is in about 36 hours time.)

The software side of things doesn't have to work perfectly - since they can discuss anything that doesn't work in their writeup, so it's easier to just do that then worry too much about getting it working.
0/0
 Reply   Quote More 

 From:  milko  
 To:  Peter (BOUGHTONP)     
32290.12 In reply to 32290.11 
I'm not sure they're nonplussed!

milko
0/0
 Reply   Quote More 

Message 32290.13 was deleted

 From:  Rich  
 To:  Peter (BOUGHTONP)     
32290.14 In reply to 32290.11 

why not make your own timestamp?

 

like [year][month][day][hour][minute][second][millisecond?]

 

2007042600153500

 

for 12:15:35.00 AM on 26th April, 2007.

 

Granted, it would require twice the storage of a unix timestamp in an INT, but it's even more flexible.

 

i.e. it would work :).


Rich - e - w - m
0/0
 Reply   Quote More 

Reply to All    
 

1–14

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats