Jump to content
grigorisk

find page by date

Recommended Posts

hi!
i have a question about find page
when i try to find a page with a value in an input field i take the right results but if i try to make the same with an input date field it not work as i want

i have two variables so my search is like this:

 

                if($articlecat!="" && $searchdate!=""){
		  $articles = $pages->find("date%=$searchdate, articleCat%=$articlecat, parent=[name=blog]");
		  
		}elseif($articlecat=="" && $searchdate!="") {
		  $articles = $pages->find("date%=$searchdate, parent=[name=blog]");
		  
		}elseif ($articlecat!="" && $searchdate==""){
		  $articles = $pages->find("articleCat%=$articlecat, parent=[name=blog]");
		  
		}else {
		  $articles = $pages->find("parent=[name=blog]");
		  
		}

this working great for tag field (articleCat) but it's not working for date field (date)

the string in $date has this format: January-2016 and in my pages has this format: 12-January-2016
so i want to take all the pages that has January-2016 on their date field

can you help me?

Share this post


Link to post
Share on other sites

Dates are not saved as text, therefore you cannot use any text based selectors. You'd need to compare them based on date values (timestamps).

  • Like 1

Share this post


Link to post
Share on other sites

You have a $searchdate of format January-2016 and the date that is saved in your page in field date is 12-January-2016?

PHP doesn't know by default that 12-January-2016 is a day within the month January of year 2016.

So you need to do some date calculations with PHP.

Try this to find out if "12-January-2016" lies within January 2016

// example code
// convert the saved date of the page to a timestamp
// $date = strtotime($page->date); // I assume the date is saved on the page in field date
$date = strtotime("12-January-2016");
// convert the $searchdate to a timestamp
$searchdate = strtotime("January-2016") + 1000;
// get first and last timestamp of the given month and year
$getdate = getdate($searchdate);
// get the month number (e.g. 1 for January)
$month = $getdate["mon"];
// get the year (e.g. 2016
$year = $getdate["year"];

// get first timestamp of that month in that year
$first = mktime(0,0,0,$month,1,$year);
// echo date('r', $first);

// get last timestamp of that month in that year
$last = mktime(23,59,00,$month+1,0,$year);
// echo date('r', $last);

// now check if $date is in the range between $first and $last
function check_in_range($first, $last, $date)
{
  // returns true if $date is in month January of year 2016 - else returns false
  return (($date >= $first) && ($date <= $last));
}

var_dump(check_in_range($first, $last, $date)); // either true or false

Now you can use check_in_range($first, $last, $date) in your if - elaseif statements.

Try the code live here.

PS: I don't know what it is, I just like to fiddle around with date calculations...

  • Like 1

Share this post


Link to post
Share on other sites
thanks @gebeer

p.s.

if anyone wants to do something like that
you have to add the gebeer code (without function in my case)
and just put this in find selector:
$articles = $pages->find("date>=$first, date<$last, parent=[name=blog], sort=date");

Share this post


Link to post
Share on other sites

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...