Date/time conversion keeps outputting NaN:NaN:NaN

I created the following code but on the front end it comes out as NaN:NaN:NaN. The format of the date time is 07/05/2021 23:59:

function my_cj($date_orig) {
    try {
    $date_time = explode(" ", $date_orig);
    $dmy = explode("/", $date_time[0]);
    return $dmy[2]."-".$dmy[1]."-".$dmy[0]." ".$date_time[1];
    } catch (Exception $e) {
    return null;
    }
}

Can anyone spot what the issue is and how to fix it?

Thanks in advance.

What are you inputting? Is it a DateTime string, or an actual DateTime Object?

If it’s a string and you are simply swapping the / for -, then a simple srt_replace() will do the trick, with no need for a custom function.

But PHP has a DateTime class to handle formatting and other DateTime related functions, so if you were to create a DateTime object, you can then use DateTime::format (date_format()) for this kind of thing.

1 Like

I can’t see anything wrong with the code you posted, what does the front end do between getting the string back from your function and displaying it? What is actually returned by your function?

“NaN” suggests “Not a Number”, which seems unlikely looking at the string you pass in, unless some other processing is going on.

It also seems unlikely that the NaNs are separated by colons, not the slashes or dashes in the code and example string.
I’m guessing there are other things at play we have not been shown.

Thanks everyone for your comments - I’m editing a pre-made theme so I’m guessing there could very well be other processing going on. What I did in the end wasn’t perfect but it got the date at least to output on the front end:

function my_cj($date_orig) {
    try {
    $date_time = explode(" ", $date_orig);
    $dmy = explode("/", $date_time[0]);
    return $dmy[2]."-".$dmy[1]."-".$dmy[0];
    } catch (Exception $e) {
    return null;
    }
}

I’m wondering if the issue was that the dates being input from the feed didn’t contain seconds only hours and minutes. I know the date format expected by the theme is year-month-date hour:minutes:seconds. As you can probably tell, coding isn’t my day job (but I do enjoy it)!

[off-topic]
@hc1972 when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.
[/off-topic]

@Gandalf sorry for that. I’ll make sure I do that next time. I’ve just added another topic which I’ve done the same with so will go back and edit that.

1 Like

I may be wrong, but I didn’t think PHP returned NaN (not a number). I thought that was just JavaScript.

Your code does not output anything with colons in it.

This is probably where your problem lies. What is “expecting” your value? What code does it use to enforce that expectation?

So… if the function receiving the date needs H:m:s, and you know that the date coming into my_cj is only H:m … you would change H:m ($date_time[1]) into H:m:00 in the output. Or, do what you’ve done above, and forget about the time altogether, and let the function assume Midnight.

1 Like

@m_hutley will give that a try. Thanks for responding.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.