0

I am using this javascript or PHP to convert a database field into a string so that I can sort on that column, however when the value of the database is null - the sorting is screwed up because I get a weird value around 1970 for the year

Does anyone know how to fix this ??

$movie_str = date('Ymd', strtotime($row['CD_MOVIE']));
7
  • 1
    Can you give us the content of $row['CD_MOVIE'] and the actual weird value Commented Mar 2, 2016 at 15:20
  • 1
    I am using this javascript to convert a database... - Er... Where's the JavaScript? Commented Mar 2, 2016 at 15:21
  • maybe its PHP - not sure Commented Mar 2, 2016 at 15:23
  • 2
    "weird value around 1970" means that strtotime() failed, returned boolean false, which was then type-cast to an integer 0 for date(), which then converted it to the epoch, Jan 1/1970 Commented Mar 2, 2016 at 15:25
  • 1
    @Rus : It's hard to tell what exactly went wrong without you telling us what value is inside $row['CD_MOVIE']. Commented Mar 2, 2016 at 15:28

1 Answer 1

3

Depending on what you want to do when your value is null, you could do something like

$val = $row['CD_MOVIE'];
$movie_str = (!is_null($val)) ? date('Ymd', strtotime($val)) : "0";
Sign up to request clarification or add additional context in comments.

3 Comments

I just want the value to be '0'
I changed my answer :)
Could be shortened to: $movie_str = (!is_null($val)) ? date('Ymd', strtotime($val)) : 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.