Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2007-03-15 16:40:27

Qwest
Member
From: London, UK
Registered: 2007-01-24
Posts: 112

Re: Sort order using a custom date field?

ruud… Slight problem!

I started adding more dates earlier today, but it’s not sorting it correctly.. For example, there’s an event on the 21st April, and one on the 13 May. 13 May will appear sooner.

Any idea how to sort the problem out?! Or do you know of a plug in that might help!?..

Offline

#14 2007-03-15 18:25:53

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Sort order using a custom date field?

If you want to order by date, you’d have to write dates like: 2007-04-21
For display purposes, you can convert that to a more pleasant looking date, using some PHP: <txp:php>echo strftime('%B %d, %G', strtotime(custom_field(array('name'=>'custom1 label'))));</txp:php>

Last edited by ruud (2007-03-16 10:45:49)

Offline

#15 2007-03-16 09:09:39

Qwest
Member
From: London, UK
Registered: 2007-01-24
Posts: 112

Re: Sort order using a custom date field?

ruud, that was what i was thinking! I didnt realise you could convert the date format via php!. I was thinking of using 2 custom fields, one entered as 2007-04-21 and then other as 21st Apr 07. The first custom field is for sorting, and the second is for display.

Will that php you’ve quoted work? Where do i put it?

Cheers :)

Offline

#16 2007-03-16 09:25:50

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Sort order using a custom date field?

I assume it works (not tested), if you adjust the “custom1 label” to match whatever label you’ve set. You can use it instead of the txp:custom_field tag you had before. The display format (%B %d, %G) is similar to the one used in the txp:posted tag (if you want to change it).

Offline

#17 2007-03-16 09:45:13

Qwest
Member
From: London, UK
Registered: 2007-01-24
Posts: 112

Re: Sort order using a custom date field?

ruud, thanks for your help so far, however there’s a slight problem… I’ve used the PHP code you gave me, and this is what im getting

Takes place on January 01, 1970

The date in the “flyer_date” custom field is 2007-04-21, yet the PHP is producing January 01, 1970

Any idea why?

Offline

#18 2007-03-16 10:24:00

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Sort order using a custom date field?

Oops, there was an error in the code… I’ve edited my earlier post.

Last edited by ruud (2007-03-16 10:46:17)

Offline

#19 2007-03-16 11:07:08

Qwest
Member
From: London, UK
Registered: 2007-01-24
Posts: 112

Re: Sort order using a custom date field?

ahhh… perfect :) Now how would i go about changing the format of the date?…oh, i get it!

thanks for your help ruud!

Offline

#20 2007-04-01 13:43:43

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

Re: Sort order using a custom date field?

ruud, I just chanced on this thread and your php 2007-04-01 to April 01, 2007 conversion snippet is just what I was looking for.
The same would work for an event end date custom field.

How about if you were to combine both start and end date? Would you have a similar trick up your sleeve?

I’m thinking along the lines of:

where same year: %B %e – %B’ %e’ %G (e.g. 30 March – 1 April 2007)
where same year and month: %e – %e’ %B %G (e.g. 1 – 4 April 2007)

That would be simply wonderful!


TXP Builders – finely-crafted code, design and txp

Offline

#21 2007-04-01 19:55:18

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Sort order using a custom date field?

jakob, that’s a bit more work. Something like this (not tested):

<txp:php>
  # convert custom fields to begin/end timestamps (easier to work with)
  $begin = strtotime(custom_field(array('name'=>'custom1 label')));
  $end   = strtotime(custom_field(array('name'=>'custom2 label')));

  if (strftime('%G', $begin) == strftime('%G', $end))
  {
    # same year
    if (strftime('%B', $begin) == strftime('%B', $end))
    {
      # and same month
      $format = '%e';
    }

    else
    {
      # but not same month
      $format = '%B %e';
    }
  }

  else
  {
    # different year
    $format = '%B %e %G';
  }

  echo strftime($format, $begin) . ' - ' . strftime('%e %B %G', $end);
</txp:php>

Last edited by ruud (2007-04-01 22:06:18)

Offline

#22 2007-04-01 21:08:50

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

Re: Sort order using a custom date field?

ruud, that looks very excellent and I can even follow it. Does it not need a closing } before the second else?

txp throws a parse error, unexpected '{' in I:\websites\uni-terra\textpattern\publish\taghandlers.php(2616) : eval()'d code on line 6
Could it be that this kind of code is not allowed within <txp:php> and it has to be put into a function as a plug-in?

BTW: I noticed that %e and %G won’t show on my XAMPP setup, but %d and &Y do. Is that windows-dependent thing as this comment on the php strftime page seems to suggest?


TXP Builders – finely-crafted code, design and txp

Offline

#23 2007-04-01 21:25:50

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Sort order using a custom date field?

Oops, it was indeed missing a closing }. Fixed in the code posted above. Using a good indenting style does help to spot these errors. I always thought it was too space consuming, but having the opening { on a separate line does make sense after all ;)

I haven’t worked with windows based hosting solutions, so I can neither confirm nor deny that %e %G work there. Personally, I prefer %Y over %G anyway. Instead of %e, you can perhaps use %#d which should have the same effect as %e according to MSDN

Last edited by ruud (2007-04-01 21:28:58)

Offline

#24 2007-04-01 21:33:22

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

Re: Sort order using a custom date field?

I get the error with the closing } too. Is that a txp:php limitation or a problem in the code? Your solution looks fine to me, so I’m wondering if it has to be moved out into a function?

I thought perhaps %#d was a typo at first, but you’re right: it works!

BTW: thank you for your help here, and thank you too for your work zem_contact_reborn. I used it today again in different guises and it’s a lovely robust solution.

Last edited by jakob (2007-04-01 22:07:08)


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB