Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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
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
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
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
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
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
Re: Sort order using a custom date field?
Oops again, there was a closing ) missing at the end of both if lines.
Offline
Re: Sort order using a custom date field?
Ah yes (slaps head). Now it works fine. Ruud, together with %#d that is just perfect!
The only other change I made except for the custom field names was in the date order "%#d", "%#d %b", and "%#d %B %Y".
Thanks for staying up late and fixing it.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Sort order using a custom date field?
Hi Ruud.
I’m sorry, but I need your help again :)
I thought this thread could answer my question, but I have an error…
I have to compare today’s date between two other dates stored within 2 custom_fields with a “if” condition.
The purpose is to calcule a reduce price (strored within a custom_field) from a period date (ie. 15-10-2207 to 15-11-2007 : this price minus 10%)
Whatever I’d try, I’ve got the same (first unixdate php) display : 01.01.1970.
Could you help me, please?
Tks Ruud by advance.
Last edited by Pat64 (2007-10-12 16:06:59)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#28 2007-12-03 13:44:31
- Qwest
- Member
- From: London, UK
- Registered: 2007-01-24
- Posts: 112
Re: Sort order using a custom date field?
ruud wrote:
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>
Ruud, help! lol!
thanks for the help you’ve provided previously. I’ve been using the above code perfectly for the last couple of months! Wooo!
However, i’m adding an event which takes place on New years eve, so the date is 31st Dec 07, or 2007-12-31 which i’ve added to the custom field. But, upon viewing the page, it shows “31st December 2008” Any idea why it’s showing 2008? I changed teh date in teh custom field to 2007-12-30, and it put it back to 2007?!
Any ideas?!
Scrub that, i had a quick gander at php.net and then changed a few bits, and it works fine now… Notably, i changed %G to %y
Last edited by Qwest (2007-12-03 13:50:21)
Offline