DotDragnet
May 21, 2012, 06:49:38 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP problem  (Read 3020 times)
robbie
Sr. Member
****
Posts: 343



View Profile Awards
« on: January 10, 2008, 08:53:14 PM »

I'm getting this problem on a site. I think this coincided with a PHP5 upgrade.

Any suggestions?

Warning: mktime() expects parameter 1 to be long, string given in .....

Thanks
Logged
Chris H
Resident God Botherer
Global Moderator
Hero Member
*****
Posts: 2291



chrishall57
View Profile WWW Awards
« Reply #1 on: January 10, 2008, 09:11:12 PM »

Well Google gives page after page of sites reporting this but not much info on what needs to be done.  shocking
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #2 on: January 10, 2008, 09:23:31 PM »

Well Google gives page after page of sites reporting this but not much info on what needs to be done.  shocking

I know Sad
Luckily a clever ddn bod is about to tell me *exactly* what to do and save the day. smile
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #3 on: January 10, 2008, 09:42:22 PM »

is there a line number with the error and if so what code is on that line, plus the line before and after. mktime() is a function that i think can be used as mktime() and that will give the time as a unix timestamp (number of seconds since 1st Jan 1970), it can also accept parameters.

Need to see the code before I can say what the problem is though.
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #4 on: January 10, 2008, 10:04:51 PM »

is there a line number with the error and if so what code is on that line, plus the line before and after. mktime() is a function that i think can be used as mktime() and that will give the time as a unix timestamp (number of seconds since 1st Jan 1970), it can also accept parameters.

Need to see the code before I can say what the problem is though.

Yep, it's on line 56 which is the last line of code in this bit...

function formatMySQLDate($inDate) {
   
    //get elements of datetime
    $arr0 = explode(" ", $inDate);
    $arr1 = explode("-", $arr0[0]);
    $arr2 = explode(":", $arr0[1]);
   
    //reformat elements
    return date("d M Y", mktime($arr2[0], $arr2[1], $arr2[2], $arr1[1], $arr1[2], $arr1[0]));   

  }

Also, when I get the error, I'm getting a date output of 01 Jan 1970

Thanks Sarah
Logged
Chris H
Resident God Botherer
Global Moderator
Hero Member
*****
Posts: 2291



chrishall57
View Profile WWW Awards
« Reply #5 on: January 10, 2008, 10:14:09 PM »

Can you check that none of your input variables is returning zero?
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #6 on: January 10, 2008, 10:16:47 PM »

Can you check that none of your input variables is returning zero?
sorry, what do you mean?

(Liking your correct use of 'is' with 'none' by the way) smile
Logged
Chris H
Resident God Botherer
Global Moderator
Hero Member
*****
Posts: 2291



chrishall57
View Profile WWW Awards
« Reply #7 on: January 10, 2008, 10:26:34 PM »

WTY!

Anyways, mktime() is deprecated as of php 5.1 and it now returns a string FALSE instead of -1 under some conditions which would explain the error message. Apparently other functions should replace it. Not sure what though.

http://no2.php.net/mktime
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #8 on: January 10, 2008, 10:45:07 PM »

mktime() isn't depreciated, only the is_dst param.

You have a null value in your database, you probably want to track down where the null is and set it to an appropriate value. And also consider some better code, no errors, and presumably today's date for null values is better than 1970.

Code:
function formatMySQLDate($inDate)
{
    if ($inDate === null) {
        return date('d M Y');
    } else {
        return date('d M Y', strtotime($inDate));
    }
}

Logged
robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #9 on: January 10, 2008, 10:49:53 PM »


You have a null value in your database, you probably want to track down where the null is and set it to an appropriate value.

Sorry, I really have no idea what I'm doing. Someone else set this up for me, I can't get hold of him, and I really need to get it solved by the morning. What does it actaully mean that I have a null value in my database.
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #10 on: January 10, 2008, 10:58:36 PM »

null = empty/zero. mktime expects to receive the following

mktime(hours, minutes, seconds, month, day, year).

So at present one of these is being given as an empty value and killing the script.

Replace the code you posted with Jason's code, that's a good start. Then you'll need to look in the database at what you're trying to retrieve, look at the date field and see if it's correct. Looking at your code it should be in the format of YYYY-MM-DD HH:MM:SS  but the record that you're requesting, or one of the records will have something that doesn't match this format, perhaps will even be empty, which may be the most likely cause.
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #11 on: January 10, 2008, 11:09:32 PM »

I've replaced that bit of code, and it all works fine! Is that it?

Thanks Jason, Sarah, Chris. All very much appreciated.
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #12 on: January 10, 2008, 11:12:45 PM »

Not necessarily it. There's still a problem with the date being retrieved. Jason's code just ensures no error displays on the website. If no date is retrieved, today's date is displayed. So you still need to investigate the db table where the records are stored and look at the datetime field and see what's going on there. Or get whoever set it up to take a look if you can.
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #13 on: January 10, 2008, 11:20:40 PM »

It doesn't have time, just date. It's a database of products. When I input a new product, I set the day, month, year. So somewhere there are products with no date, thus throwing it out?
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2174



View Profile WWW Awards
« Reply #14 on: January 10, 2008, 11:37:06 PM »

Your function that you originally posted expected a date and time in the value provided to it. So even though you're just setting the day month and year, I can only presume that the field is storing the time perhaps as 00:00:00. However that's just a guess. But yes, if you're in charge of adding a date then i guess the day, month or year wasn't set at some point and so the value hasn't gone in correctly, or at all.

If this came up when viewing a specific product then you'll need to check on the details either via your admin or in the database via phpmyadmin. If it was on a results page for example, then you'll need to check all recently added products if this is only a recent problem.
Logged

robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #15 on: January 10, 2008, 11:41:41 PM »

The thing is, I can't see how the date wasn't entered. I use an input page, where the date is selected as an option. It affected every results page, regardless of when it was entered. The only change was a move to PHP5 as far as I can tell
Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #16 on: January 10, 2008, 11:47:59 PM »

The code you posted was for parsing a DATETIME value (2008-01-10 23:30:15), but if your database is a DATE field (2008-01-10) then the error (ie the hour value being empty) would be the same as if the field were a null value.

Presumably PHP4 mktime() had silently converted "" and null to 0 (midnight), and your error_reporting didn't cover E_NOTICE to complain of the missing array index, so you never noticed. You probably have nothing to worry about.
Logged
robbie
Sr. Member
****
Posts: 343



View Profile Awards
« Reply #17 on: January 11, 2008, 08:00:50 AM »

Makes sense. Thanks again Jason.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!