Function in an array (unexpected '=>' syntax error)

Would like to remove the seconds from SQL table “time_value” 's output: hh:ii:ss–> hh:ii
(data type TIME)
Appreciate any help :slight_smile:

  <?=$events['e'][$eid] ['time_value' => function ($formattime){
                                              echo $formattime ->format('hh:ii');
                                                                }
                        ];
   ?>

Well, look up the data() functions to get the format you wish, but, it looks loosely like this:

echo date( 'g:ia', strtotime($time_value) );

But, you can not do it inside a function inside an array. This code is badly formatted.

If the data is coming from a database, just SELECT it in the format you want. No need for code gymnastics.

yes, thought about anonymous functions - do not seem to work though.

echo date( 'g:ia', strtotime($time_value) ); before =$events [ ..] has no effect.

also tried
$date = new DateTime( $time_value); echo $date->format('g:i');
which throws an undefined function error.

how could that function look like outside =$events [ ..] ?

time_value is the SQL table’s name. Time input (hh:ii) comes from
<input type="time" id="evttime">

Selected “Date/Format” in transformation dropdown and put in “hh:ii” in input transformation options for table time_value.
This saves (date and) time in hh:ii in table rows. Throws seconds in the output though.

SELECT DATE_FORMAT(YourColumName, "%d/%m/%Y %l:%i %p") FROM YourTableName;

https://www.mysqltutorial.org/mysql-date_format/

1 Like

You can not have functions created inside an array. They will never be accessible.
Also, you are pulling out data from the $events array, but try to alter it in the array. No go!
First, you should not use shortcodes. = is print or better echo…

To print or display something in an array, you might do this:
echo $events['e][other-dimensions-of-array];
This displays (prints) the value inside of the array.

For a function, you might have something like:
function some-function-name($formattime) {
some function code…
}
You would NOT put a function inside of an array. I will guess you are doing this for a class and do not have any understanding on functions. I am guessing someone said to do it that way. So, to use your own function, it would be declared before you ever use it. Like I showed before. Then, you would use that function inside your echo line. Do you understand that?

1 Like

Hello,

I think what you are looking for is:

$events['e'][$eid] = ['time_value' => function ($formattime) {
        echo $formattime->format('H:i');
    }
];

The error is obviously a syntax error - the previous notation will not work the way you constructed it.

Also I believe the format you need is H:i for hours and minutes, without seconds.

1 Like

thanks, this throws “array” as output instead of any time.
any idea whats happening here?

Formatting eg current time in another div, works like a charm, need to change the “time_value’s” output in this array though.

SELECT DATE_FORMAT has no effect at all.

Sponsor our Newsletter | Privacy Policy | Terms of Service