• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reformat date from CURRENT_TIMESTAMP

#1
quick question: is it possible to reformat a CURRENT_TIMESTAMP generated timestamp from MySQL



Code:
Y-m-d h:i:s

to

d.m.Y h:i:s



in parser? I cannot find any Provided Filter.

Example, but that doesn't work:


Code:
{changetable}

                  <tr>
                    <th scope="row">{timestamp|date(Y.m.d h:i:s) }</th>
                    <td>{message}</td>
                  </tr>

{/changetable}
Reply

#2
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);
download and make your system directory strict
Reply

#3
(02-22-2020, 05:26 PM)John_Betong Wrote: 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);


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'));
Reply

#4
I believe that spaces are not allowed either following or preceding curly braces. Try removing the space befor the closing curly brace.
download and make your system directory strict
Reply

#5
unfortunately not. the parser seems not to be able to read the date correctly

[Image: e11669608094c225f8863c69d9611b7a48b1667b.png]


I write: {timestamp} alone it works


[Image: a3abe03ada573c8180d67a7ee388c88dc5f83a88.png]
Reply

#6
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!
Reply

#7
@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>
  <th scope="row">
    <?php date('d.m.y H:i:s', $timestamp); ?>
  </th> 
</tr> 
download and make your system directory strict
Reply

#8
(02-23-2020, 09:47 PM)John_Betong Wrote: @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>
  <th scope="row">
    <?php date('d.m.y H:i:s', $timestamp); ?>
  </th> 
</tr> 


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 {}
Reply

#9
(02-23-2020, 03:35 PM)zahhar Wrote: 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!



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:

[Image: 1d4083e045820eb5133a2dedd0696826e2042a68.png]
Reply

#10
(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?

my table:

[Image: 1d4083e045820eb5133a2dedd0696826e2042a68.png]

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() {
        $query $this->db->query('SELECT CURRENT_TIMESTAMP as ts');
        $row  $query->getRow();
        return $row->ts;
    

Then in the controller:
PHP Code:
public function test_parser() {
        $parser = \Config\Services::parser();
        $mymodel = new MyModel();

        $data = [
            'timestamp'     => $mymodel->getTime()
        ];

        return $parser->setData($data)->render('test_date_filter');
    

And, finally, in the View:

Code:
<html>
<body>
        <p>Original: {timestamp}</p>
        <p>Filtered: {timestamp|date(d.m.Y H:i:s)}</p>
</body>
</html>

Here is what I get (retina image below, sorry for the small size):

   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.