Please note that Form2Content is no longer being developed. All files from the latest version can be found on Github.

Form2Content Forum

Questions and answers for Form2Content, a Joomla CCK.
  1. Sebastian Fischbach
  2. F2C Search settings
  3. Tuesday, 30 July 2019
  4.  Subscribe via email
Hello guys,

I'm trying to make use of the custom date interval field. Thanks for the provided PHP example! However, with my limited knowledge of PHP, I'm unable to adapt it to my needs.

What I want: to make users choose between different ages of documents. I need a selection of:

  • a week (seven days backwards from the then current date)
  • a month
  • a year
  • two years
  • five years
  • doesn't matter (any date between now and the beginning of time, 1980 or a similar year would be sufficient ;) )


What I've tried:

$currentDate = date('Y-m-d');

$intervalDates = [
$currentDate.'|'.date('Y-m-d', time()-604800) => "a week",
$currentDate.'|'.date('Y-m-d', time()-2678400‬) => "a month",
$currentDate.'|'.date('Y-m-d', time()-31536000) => "a year",
$currentDate.'|'.date('Y-m-d', time()-63072000‬) => "two years",
$currentDate.'|'.date('Y-m-d', time()-157680000) => "five years",
];


I know it's probably not elegant, but also it's not working. :p
Do you have any suggestions to improve my beginners code?

Best regards,
Sebastian
Accepted Answer
Accepted Answer Pending Moderation
Hi Sebastian,

Here's a small example of a few options that you want:


$tomorrow = new JDate('now +1 day');
$weekAgo = new JDate('now -1 week');
$yearAgo = new JDate('now -1 year');
$twoYearsAgo = new JDate('now -2 year');

$intervalDates[$weekAgo->format('Y-m-d').'|'.$tomorrow->format('Y-m-d')] = '1 week ago';
$intervalDates[$yearAgo->format('Y-m-d').'|'.$tomorrow->format('Y-m-d')] = '1 year ago';
$intervalDates[$twoYearsAgo->format('Y-m-d').'|'.$tomorrow->format('Y-m-d')] = '2 years ago';


For the "doesn't matter" option, I wouldn't create a specific dropdown list entry, but create an empty option.
That way no filter gets applied to the articles.

Best,
Julien
  1. more than a month ago
  2. F2C Search settings
  3. # Permalink
Responses (2)
Accepted Answer Pending Moderation
Thank you very much Julien, it works as expected and looks much nicer than my "five years in seconds" calculation. :D I didn't know that Joomla offers help with regards to date formatting and calculation (in case somebody else didn't know: https://docs.joomla.org/How_to_use_JDate).

Best regards,
Sebastian
  1. more than a month ago
  2. F2C Search settings
  3. # 1
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.