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.
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:
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.
@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.
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.