Reformat date from CURRENT_TIMESTAMP |
quick question: is it possible to reformat a CURRENT_TIMESTAMP generated timestamp from MySQL
Code: Y-m-d h:i:s in parser? I cannot find any Provided Filter. Example, but that doesn't work: Code: {changetable}
The timestamp could be passed to the PHP day(...) function. Check out the examples:
https://www.php.net/manual/en/function.date.php Code: echo date('Y.m.d H:i:s', $timestamp); (02-22-2020, 05:26 PM)John_Betong Wrote: The timestamp could be passed to the PHP day(...) function. Check out the examples: hello john. unfortunately this is not possible, because the information is passed as array to the parser. $data = [ 'ma_perm_name' => permission2name(session('userData.permission')), 'breadcrump_nav_big' => 'Meine Daten', 'breadcrump_nav_low' => 'Profile', 'changetable' => changetable_get(session('userData.id'), 'changetable'), ]; // view template echo $parser->setData($data)->renderString(view('profile'));
I believe that spaces are not allowed either following or preceding curly braces. Try removing the space befor the closing curly brace.
unfortunately not. the parser seems not to be able to read the date correctly
![]() I write: {timestamp} alone it works ![]()
Dear MikiStoni,
Seems you spotted a bug in CI4 Parser class (vendor/codeigniter4/framework/system/View/Parser.php). I can reproduce it. This code works fine: Code: {timestamp|date(d m Y H:i:s)} However if you add dots, it would not work and "bypasses" Parser unchanged: Code: {timestamp|date(d.m.Y H:i:s)} I digged into CI4 Parser class, debugged it a bit and I see that {timestamp} variable does not reach filters when it has a dot. It gets stuck in replaceSingle function that parses variable into pieces using regexp defined earlier in the parseSingle function of the same class. This is how pattern is defined: Code: $pattern = '#' . $this->leftDelimiter . '!?\s*' . preg_quote($key) . '\s*\|*\s*([|a-zA-Z0-9<>=\(\),:_\-\s\+]+)*\s*!?' . $this->rightDelimiter . '#ms'; You can see, there is no dot symbol in the pattern. Add it anywhere inside the square brackets, and it will work. My example (dot added before coma): Code: $pattern = '#' . $this->leftDelimiter . '!?\s*' . preg_quote($key) . '\s*\|*\s*([|a-zA-Z0-9<>=\(\).,:_\-\s\+]+)*\s*!?' . $this->rightDelimiter . '#ms'; I will work more on testing different options and once I am sure it has no side effects will submit Pull Request to CI4 git to get it fixed. Thank you for spotting it, indeed very important finding before final release of CI4!
@MikiStoni,
> unfortunately not. the parser seems not to be able to read the date correctly I am not a fan of using any parser and have never heard an argument that has convinced me to change my mind. Try the following: Code: <tr> (02-23-2020, 09:47 PM)John_Betong Wrote: @MikiStoni, is surely a matter of taste. i like to use parsers. for me it is more tidy. i create an array and pass it to the template. the foreach and formatting is done in the template. i always used smarty. and with ci4 i wanted to eliminate smarty. but simple functions like formatting date or substr functions are unfortunately not supported. by the way, your code does not work, because i pass the data as an array. variables in parser are only readable with brackets {} (02-23-2020, 03:35 PM)zahhar Wrote: Dear MikiStoni, hi thanks for your info. unfortunately the workaround does not work for me. in which format did you save the date in the DB? my table: ![]() (02-24-2020, 08:34 AM)MikiStoni Wrote: thanks for your info. unfortunately the workaround does not work for me. in which format did you save the date in the DB? How did you implement my fix? I am sure it works. See code here, I created a PR: https://github.com/codeigniter4/CodeIgni...2603/files This is code for the model. Note I am using mySQL CURRENT_TIMESTAMP function that outputs same format as you have, e.g. YYYY-MM-DD HH:MM:SS PHP Code: public function getTime() { Then in the controller: PHP Code: public function test_parser() { And, finally, in the View: Code: <html> Here is what I get (retina image below, sorry for the small size): |
Welcome Guest, Not a member yet? Register Sign In |