Jump to content

Preview/Discussion: RockDaterange. Fieldtype + Inputfield to easily pick daterange or timerange


bernhard

Recommended Posts

How crazy is that piece of software called ProcessWire?? Exact matches are now possible using the string notation of date ranges. Writing this down and looking at the screenshot I realize that it would make a lot of sense to be consistant in the format when transforming a RockDaterange object into a string and vice versa.

6UMn6eO.png

Amount of code necessary to support this? 10 Lines ?

  • Like 2
Link to comment
Share on other sites

On 2/23/2020 at 5:02 PM, bernhard said:

Writing this down and looking at the screenshot I realize that it would make a lot of sense to be consistant in the format when transforming a RockDaterange object into a string and vice versa.

I've put huge amounts of work into that module in the last few days. I had to take two steps back to take the next step forward: It turned out, that I needed a more robust and flexible setup when dealing with date ranges. And what I did is - thinking about it now - quite obvious:

A RockDaterange now consists of two RockDatetimes.

I've put a lot of effort into developing the RockDatetime module. It is already available on Github and has lots of examples and docs in the readme: https://github.com/BernhardBaumrock/RockDatetime

Highlights:

// calculating FROM/TO for RockDaterange
case 'hour':
  $from = new RockDatetime("{$obj->date} {$obj->time}:00:00");
  if($isTo) $to = $from;
  else $to = $from->copy()->move("+1 hour");
  break;

// check if FROM starts at 00:00 on that day
if($this->from->equals($this->from->firstOfDay()) ...

// formatting
$d = new RockDatetime("25.02.2020 14:00");
echo $d->format(); // 25.02.2020 14:00
echo $d->format("%A, %d.%m.%y (%H:%M Uhr)"); // Dienstag, 25.02.20 (14:00 Uhr)
echo $d->format(['time' => "%H|%M|%S"]); // 25.02.2020 14|30|00

 

 

I've also totally rebuilt the date parsing engine!

Finding pages using the API

$tpl = "template=event";
$pages->find("$tpl,range=2020"); // find all events in 2020
$pages->find("$tpl,range=2020-02"); // find all events in Feb 2020
$pages->find("$tpl,range=2020-02-26"); // find all events on 26. Feb 2020

// other possible selectors
$pages->find("$tpl,range=2020-02-10 - 2020-02-20");
$pages->find("$tpl,range=2020-02-10 7:30 - 17:30");
$pages->find("$tpl,range=2020-02-10 7 - 9");
$pages->find("$tpl,range=2020-02-10 12:00 - 2020-02-20 18:00");

// find events in Feb 2020 sorted by range duration
$pages->find("$tpl,range=2020-02, sort=-range.duration");

 

Creating pages using the API

$p = new Page();
$p->setAndSave("range", "2020");
// from = 2020-01-01 00:00:00
// _to = 2020-12-31 23:59:59
// to = 2021-01-01 00:00:00

$p->setAndSave("range", "2020-12-24 16:00 - 18:00");
// from = 2020-12-24 16:00:00
// _to = 2020-12-24 17:59:59
// to = 2020-12-24 18:00:00

 

Formatting in action:

7V5blAC.png

// left column
echo $range->format(['date' => "%A, %d.%m.%Y", 'time' => '']);

// right column
echo $range->format(['date' => "%a, %d.%m.%Y"]);

Note that the last date does NOT show "07.03.2020 09:00 - 07.03.2020 12:00" - since the event ends on the same day as it starts we don't show that date again. RockDaterange will take care of all those tedious details!

  • Like 4
  • Thanks 2
Link to comment
Share on other sites

1 hour ago, bernhard said:

I've put a lot of effort into developing the RockDatetime module. It is already available on Github and has lots of examples and docs in the readme: https://github.com/BernhardBaumrock/RockDatetime

Thank you so much for all your efforts in putting this together! I'll be using this soon to replace date range logic in 2 live projects that deal with events.

  • Like 1
Link to comment
Share on other sites

54 minutes ago, bernhard said:

t would be very easy to create a RockDatetime field based on the core Datetime field but waking up having a RockDatetime object instead of a timestamp. Maybe you want to experiment with that and create a PR?

I thought I'd just use your new inputfield type ? Won't have time for implementing this before April. If I see room for improvement on your module, I am happy to make a PR. Thanks again!

Link to comment
Share on other sites

RockDatetime is not an Inputfield. It is just a PW module that returns a RockDatetime object you can use for whatever you want. You could use the core InputfieldDatetime though and turn it into a RockDatetime like this:

$d = new RockDatetime($page->getUnformatted('datefield'));
echo $d->format();

Range input/output is not part of RockDatetime!

Link to comment
Share on other sites

@d'Hinnisdaël mentioned in the recent new post that there's a library called "carbon" for managing date/times. I've put together a module to include that library into PW for quick testing if anybody is interested: https://github.com/BernhardBaumrock/RockCarbon

Using this library handling timezones should be quite straightforward. Dates in the DB could be stored in UTC format and displayed based on the users/systems timezone:

FnUTZ0A.png

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

@bernhard

I was playing around with your RockDatetime module and i have problems getting the right locale.
I need german and the the results are all english. Do i have to set language somewhere else other than html lang="de"?

$d = new RockDatetime($event->getUnformatted('event_startdate'));
$format = "Jeden %A, %H:%M Uhr";
echo $d->format($format); // Jeden Thursday, 10:00 Uhr !

 

Link to comment
Share on other sites

1 hour ago, DavidBerlin said:

Do i have to set language somewhere else other than html lang="de"?

Well, that's in your HTML. PHP doesn't care about that, or even knows about it ?

Set it somewhere in your template, at the top (if you have a multi-lang setup, you probably need to detect the user language first)
https://www.php.net/manual/en/function.setlocale.php

or globally in site/config.php

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hi @LuisM

Thank you, that's a known issue https://github.com/BernhardBaumrock/RockDatetime/issues/1 and thx to @Jan Romero we already know the reason. I have some more important stuff to do at the moment and I think I'll refactor my module to use the https://carbon.nesbot.com/ library for handling single timestamps as it has everything already done and is well tested and maintained ? 

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Hello @bernhard, this field looks great! Is it possible for us to use it as well? I would like to implement it in a current project and it seems like it will work perfectly.
I saw the module on github but is the inputField also available?

Edited by michelangelo
didn't look through the end of the thread
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

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
  • Recently Browsing   0 members

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