Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2013-07-20 15:04:32

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

Version 0.2.8: easier dates comparison:

<txp:etc_if_date date="expires" compare="now..+3 days" format="%s">
	This article expires within 3 days!
</txp:etc_if_date>

Offline

#14 2013-07-20 17:09:27

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

Version 0.2.9: and a new <txp:etc_date_diff /> tag:

<txp:etc_if_date date="expires" compare="now..+3 days" format="%s">
	This article expires in less than <txp:etc_date_diff from="yesterday" to="expires" /> days!
</txp:etc_if_date>

Offline

#15 2013-12-20 14:34:18

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

Version 0.3: introduces date ranges, so it’s possible to output date lists like

<txp:etc_date date="today..expires" />

Now can be used as container too:

<txp:etc_date date="sunday..sunday +6 days" wraptag="p" break=", ">
	<txp:etc_date timestamp='<txp:etc_timestamp />' format="%A" /> -
	<txp:etc_date timestamp='<txp:etc_timestamp />' format="%A" lang="ru_RU" />
</txp:etc_date>

outputs

Sunday - Воскресенье , Monday - Понедельник , Tuesday - Вторник , Wednesday - Среда , Thursday - Четверг , Friday - Пятница , Saturday - Суббота

Offline

#16 2017-01-30 12:52:12

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: etc_date : custom dates and calendars

Hi Oleg,

I saw your example of a countdown above, and you date + x days example with etc_date too.

Does that mean, you can add a number of days to an existing date to make a message expire automatically after x days, or to begin a message the day after a date?

An example: I have a course site with key dates stored as variables, and a little self-built macro, txp:datum, that pulls out the variables and reformats them in different formats for use in the text or calendar.

Would the following be correct?

Example: begin a message a day after a submission deadline

<txp:etc_if_date value='<txp:datum date="apply-by" format="%s" /> +1 day..<txp:datum date="confirm-by" format="%s" />' format="%s">
  <p>Applications are now closed. We will inform successful applicants shortly.</p>
</txp:etc_if_date>

Example: final message that should begin a day after the course is over and auto-expire 10 days later

<txp:etc_if_date value='<txp:datum date="course-end" format="%s" /> +1 day..<txp:datum date="course-end" format="%s" /> +10 days' format="%s">
   <p>The course is over. Thank you to all who came.</p>
</txp:etc_if_date>

Have I understood that correctly?


TXP Builders – finely-crafted code, design and txp

Offline

#17 2017-01-30 14:45:09

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

Hi Julian,

This will not work, since

<txp:etc_if_date format="%s"
    value='<txp:datum date="apply-by" format="%s" /> +1 day..<txp:datum date="confirm-by" format="%s" />' />

will try to literally compare the current %s-formatted date (say, 1485788875) with something like 1485780000 +1 day.

Additionally, strtotime function used in etc_date will not correctly interpret 1485780000 +1 day. You need to change the format to get some English date from <txp:datum /> and use compare attribute, something like

<txp:etc_if_date compare='<txp:datum date="apply-by" format="%c" /> +1 day..<txp:datum date="confirm-by" format="%c" />'>
  <p>Applications are now closed. We will inform successful applicants shortly.</p>
</txp:etc_if_date>

Hope it helps.

Offline

#18 2017-01-31 09:58:32

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: etc_date : custom dates and calendars

Brilliant, thank you – I wasn’t sure which strftime combinations worked. My ‘datum’ macro has some plain-language attributes/presets (deutsch, deutsch_text, etc.) for easier use by editors in articles, but can output any strftime date format.

As I don’t have any times set in the variables, can I also use %x instead (and is that better or worse)?
Or would it be better to explicitly specify a certain output given that my server is based in Germany and I’m not sure if that locale plays well with +1 days, etc. – and if so, what’s best %m/%d/%Y (America) or %d/%m/%Y (British) or %F (aka %Y-%m-%d)?

The other thing I haven’t quite worked out with etc_date is which attributes to use when, and in which combination. In your last example, you have compare without a date attribute. I didn’t realise that was possible. Am I right in assuming that when no date is given, it compares against the current server date?


TXP Builders – finely-crafted code, design and txp

Offline

#19 2017-01-31 10:45:27

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

AFAIK, strtotime is not localized, so you’d better use something like %F, which should be ok.

jakob wrote #303750:

Am I right in assuming that when no date is given, it compares against the current server date?

Completely, be it with value or compare. The difference between value and compare is that value will be used as is for comparison with formatted date, while compare will be transformed (by strtotime) into UNIX timestamp, as well as date.

Offline

#20 2017-01-31 11:19:44

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: etc_date : custom dates and calendars

etc wrote #303752:

AFAIK, strtotime is not localized, so you’d better use something like %F, which should be ok.

Thanks. That’s what I’d read too.

Completely, be it with value or compare. The difference between value and compare is that value will be used as is for comparison with formatted date, while compare will be transformed (by strtotime) into UNIX timestamp, as well as date.

Apologies if I’m being dense here but in case I’ve got confused here, do you mean:

  • Use value where your date string (or from..to combo) exactly matches the format specified in the format attribute, and
  • Use compare where your date string first needs computing (e.g. has a +1 day / +1 week / +10 days …) prior to comparison.
  • Use date attribute to compare against a specific date. If omitted, the server date (e.g. now/today) is used.

Sorry for all the questions, but it’s not so easy to test whether everything’s working correctly when testing different if-constructions against server time. I can only specify a date attribute to compare against, then remove it again and hope it will work correctly when the time comes…


TXP Builders – finely-crafted code, design and txp

Offline

#21 2017-01-31 13:02:38

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

No worries, sorry for often being terse. I have a bit forgotten myself how it works, but you got it all right: value is not computed, but compare is (note that date is always computed).

Let us add two examples, for the record:

  • you need to detect if tomorrow is a working day (from Monday to Friday):
    <txp:etc_if_date date="tomorrow" format="%u" value="1..5" />
    will compute tomorrow weekday number and check if it is between 1 and 5 (inclusively)
  • you need to detect whether Christmas is within 3 days:
    <txp:etc_if_date date="25 December" format="%s" compare="-3 days..+3 days" />
    will compute and compare the timestamps of 25 December and +-3 days (from now). Here we don’t know which value +-3 days will have, so we need to compute it first.

Offline

#22 2017-01-31 13:11:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: etc_date : custom dates and calendars

Thanks, having that discussion helped me understand it better. If you like – once I’ve tested my code properly – I can contribute some examples for your plugin help.


TXP Builders – finely-crafted code, design and txp

Offline

#23 2017-01-31 15:18:36

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: etc_date : custom dates and calendars

jakob wrote #303758:

If you like – once I’ve tested my code properly – I can contribute some examples for your plugin help.

You are welcome! Just fwiw, in 4.7 we have enhanced month and time attributes of article(_custom). It can retrieve articles posted between two (absolute or relative) dates now, though it probably does not help in your case.

Offline

#24 2017-10-08 05:40:52

raminrahimi
Member
From: India
Registered: 2013-03-19
Posts: 278

Re: etc_date : custom dates and calendars

how to convert comment date to jalali format ?

Offline

Board footer

Powered by FluxBB