grigorisk Posted January 22, 2016 Share Posted January 22, 2016 hi!i have a question about find pagewhen 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 wanti 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-2016so i want to take all the pages that has January-2016 on their date fieldcan you help me? Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 22, 2016 Share Posted January 22, 2016 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). 1 Link to comment Share on other sites More sharing options...
gebeer Posted January 23, 2016 Share Posted January 23, 2016 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... 2 Link to comment Share on other sites More sharing options...
grigorisk Posted January 26, 2016 Author Share Posted January 26, 2016 thanks @gebeerp.s.if anyone wants to do something like thatyou 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"); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now