Jump to content

output relativeTime - hook WireDateTime?


August
 Share

Recommended Posts

Hey Guys,

I am new to Processwire at times. I like it and talk about. Sometimes I  stumbling, over and over, especially on  modifications  ...
I would like to customize the output without having to use the translator module.

I get the result with uikit.php.

$date = $page->get('date');  
$dateE = $page->get('date_end');. 

The rendered Output looks like:
       image.png.f06678cbfe6b2ad2c3c34173eb09cf05.png

Without entering WireDateTime.php directly (of course, I don't want to do code manipulation here)  and depending on what situation is given, getting an output like this:
"24.09.2020, nur noch 12 Stunden, es geht gleich los!" is what I'm looking for.
From what I've read so far, a hook "After" in ready.php would be just right. B  ut, I have no exact idea how to overwrite the standard output now.

I think, I don't need this Module: https://modules.processwire.com/modules/fieldtype-datetime-advanced/
Then I found Ryans page about "$datetime->relativeTimeStr()" : https://processwire.com/api/ref/wire-date-time/relative-time-str/ but I miss a more  practical Example, using such method.

It would be great if someone could guide me.
Thank you for your great efforts.

Edited by August
correction
Link to comment
Share on other sites

1 hour ago, August said:

"24.09.2020, nur noch 12 Stunden, es geht gleich los!" is what I'm looking for.

I don't know what this means :-). However, you can always get the raw date and format it as you wish. 

$dateUnformatted = $page->getUnformatted('date');// e.g. 1576236300

 

Link to comment
Share on other sites

As @kongondo already mentioned, you can get the unformatted value, and then simply do all the desired calculations you need.

$ts = $pages->get(12222)->getUnformatted("proj_timespan_to");
$diffHours = (int) round(abs(time() - $ts) / (60*60));
$diffDays = (int) round(abs(time() - $ts) / (60*60*24));

You'll find plenty of documentation about PHP functions, e.g. here or here.

Link to comment
Share on other sites

Thank you for your answers. Yes, I can see the timestamp that way, kongondo.
What I ment, is ""Only 12 hours left, it's about to start!" is necessary and should be the right output". ?

Currently I don't need calculate further, because Ryan has implemented relativeDate in WireDateTime.php, thats more than enough exactly.

Before I tried things like this, dragan, I calculate a little too, in my template home.php:

use DateTime;

$stDate  = new DateTime("2020-09-21");
$ndDate = new DateTime("$dt");
$intvl = $stDate->diff($ndDate);
echo "<br>" . $intvl->y . " Jahr, " . $intvl->m." Monate ".$intvl->d." Tage";
echo "\n";
// Total amount of days
echo ", also " . $intvl->days . " Tage," . " zum Datum: " . $dt . ".";

I'm just looking only for a practical solution to adjust(translate/modify) the relative time pre-formatting that I have set within:

image.thumb.png.87711fbad723cea633d079452e8ae5fb.png

for example: which are stored in   protected function getPeriods()  as arrays ( in WireDateTime.php ) :
    medium-plural' => array(
                $this->_("secs"),  // should be "Sek."
                $this->_("mins"),  // should be "Min."
                $this->_("hrs"),   // should be "std."
                $this->_("days"),  // should be "Tage"
                $this->_("weeks"), // should be "Wochen"
                $this->_("months"),// should be "Monate"
                $this->_("years"), // should be "Jahre"
                $this->_("decades") // "Jz."
            ), ...

Thanks in advance!

Link to comment
Share on other sites

You should be able to translate these strings in the language section: setup/language-translator/edit/?language_id=1010&textdomain=wire--core--wiredatetime-php

Just replace the language_id with your alternative language id. Or go to setup > languages > German and click on "find files to translate" under "core translation files", then select WireDateTime.php.

  • Thanks 1
Link to comment
Share on other sites

dragan, I haven't installed that Language Support.

As Ryan explains on Hooking after "The 'after' hooks are called immediately after each $datetime->relativeTimeStr(…) method call is executed. This type of hook is especially useful for modifying the value (does it mean the predefined strings too?) that was returned by the method call."

Using the hook "after" would be really a nice Solution, because it is not required, translate the whole page, that might the reason for installing that Language-Module.

Link to comment
Share on other sites

4 hours ago, dragan said:

You should be able to translate these strings in the language section: setup/language-translator/edit/?language_id=1010&textdomain=wire--core--wiredatetime-php

Just replace the language_id with your alternative language id. Or go to setup > languages > German and click on "find files to translate" under "core translation files", then select WireDateTime.php.

Thank you!
image.thumb.png.4641dcf0315815587e92e6ef959f9203.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...