Roych Posted August 20, 2020 Share Posted August 20, 2020 Hello, I need some help with coding please as Im not very good at it ? I'm working with some custom event calender and I have two datetime fields (Start_date, End_date). I don't want my "End_date" to be mandatory, but I get warning in front end if I don't populate it. How would I fix this. (some if else, or ...?) I'm echoing some extra text if event date is today, or it still lasts (actualy kinda works ?). My code atm: the problem is $end = date('d. m. Y', $single->getUnformatted('End_date')); I guess! Spoiler <?php $today = date('d. m. Y'); $start = date('d. m. Y', $single->getUnformatted('Start_date')); $end = date('d. m. Y', $single->getUnformatted('End_date')); if($today == $start AND $today == $start) echo " <div class='concert-info'> <b>Poteka:</b> <span> <span><i class='fa fa-hand-o-right' aria-hidden='true'></i> Danes!</span> </span> </div> "; elseif ($today >= $start AND $today <= $end) echo " <div class='concert-info'> <b>Traja do:</b> <span> <span><i class='fa fa-exclamation' aria-hidden='true'></i> Poteka do $end!</span> </span> </div> "; ?> Warning I get is: Spoiler Warning: date() expects parameter 2 to be integer, string given in ... /site/templates/calendar-list.php on line 67 Thank you very much R Link to comment Share on other sites More sharing options...
BillH Posted August 20, 2020 Share Posted August 20, 2020 You need to check whether there's something in the end date field. Then, if you're going to use the value in a function, you need to give it a suitable value if there's nothing in the field. For example: if ($single->End_date) { $end = date('d. m. Y', $single->getUnformatted('End_date')); } else { $end = date('d. m. Y', 0); // This would be 01.01.1970, but you might want something in the future } I also note that you are getting the unformatted dates from the fields and then formatting them again with the date() function. As you are just comparing values (at least in the code you have posted), you could simply use the values from the fields, or the unformatted value if it's easier to work with UNIX times. And you have $today == $start twice in your first 'if' condition! 1 Link to comment Share on other sites More sharing options...
Roych Posted August 20, 2020 Author Share Posted August 20, 2020 Thank you ? Looks fine and no error anymore. ? I have $start twice because if there is only one date today selected my extra text didn't show. R Link to comment Share on other sites More sharing options...
zoeck Posted August 20, 2020 Share Posted August 20, 2020 26 minutes ago, Roych said: I have $start twice because if there is only one date today selected my extra text didn't show. This makes no sense... if($today == $start AND $today == $start) This is redundant, it is simply 2x the same check Use this: if($today == $start) 2 Link to comment Share on other sites More sharing options...
Roych Posted August 21, 2020 Author Share Posted August 21, 2020 18 hours ago, zoeck said: This makes no sense... if($today == $start AND $today == $start) This is redundant, it is simply 2x the same check Use this: if($today == $start) You are right, thank you .. Not sure what I was doing realy ? R Link to comment Share on other sites More sharing options...
Roych Posted August 25, 2020 Author Share Posted August 25, 2020 Hello, I got lost, need help again with this, please ? I have "Start_date" and "End_date" fields and sometimes event lasts for more than one day. I would like it to be shown to the "End_date" and then it would be nice if it got "hidden" (hidden not deleted in backend). How would I achieve this. Atm Im using "Start_date=today" for hiding events in frontend, but if it has End_date set, it dissapears also when Start_date ends, I don't want this. Is it maybe also possible to set the past event in Hidden in backend after End_date or if just Start_date is set? <?php $events = $pages->find('template=calendar-post, Start_date>=today, sort=Start_date, limit=10'); ?> My entire code here: Spoiler <?php $events = $pages->find('template=calendar-post, Start_date>=today, sort=Start_date, limit=10'); ?> <?php if(count($events)): ?> <?php foreach($events as $single): ?> <div class="msl-concert-list"> <?php if($single->images) echo "<figure><img src='{$single->images->first->url}' alt='{$single->images->first->description}' class='event-list-img'></figure>";?> <div class="text-overflow"> <h4 class="concert-title col-md-12 col-sm-12 col-xs-12"><a href="<?=$single->url?>"><?=$single->title?></a></h4> <!--Concert Meta Start--> <div class="concert-meta"> <div class="col-md-5 col-sm-5 col-xs-5"> <div class="concert-info"> <b>Datum:</b> <span><?php echo strftime("%A", strtotime($single->Start_date)); ?>, <?php echo strftime("%d %B %Y", strtotime($single->Start_date)); ?></span> </div> <div class="concert-info"> <b>Ura:</b> <span><?php echo strftime("%H:%M", strtotime($single->Start_date)); ?> </span> </div> </div> <div class="col-md-7 col-sm-7 col-xs-7"> <div class="concert-info"> <b>Lokacija:</b> <span><?=$single->lokacija?><?=$single->locationMap->address; ?></span> </div> <!-- Calendardar extra text --> <?php $today = date('d. m. Y'); $start = date('d. m. Y', $single->getUnformatted('Start_date')); if ($single->End_date) { $end = date('d. m. Y', $single->getUnformatted('End_date')); } else { $end = date('d. m. Y', 0); // This would be 01.01.1970, but you might want something in the future } if($today == $start AND $today == $end) echo " <div class='concert-info'> <b>Poteka:</b> <span> <span><i class='fa fa-hand-o-right' aria-hidden='true'></i> Danes!</span> </span> </div> "; elseif ($today >= $start AND $today <= $end) echo " <div class='concert-info'> <b>Traja do:</b> <span> <span><i class='fa fa-exclamation' aria-hidden='true'></i> $end!</span> </span> </div> "; ?> <!-- Calendar extra text END --> </div> </div> <!--Concert Meta End--> <a class="btn-1 theme-bg" href="<?=$single->url?>">Beri več</a> </div> </div> <!--Event End--> <?php endforeach; ?> <?php else: ?> <div class="row"> <button type="button" class="btn btn-dark"><strong>Glavo pokonci!</strong> Trenutno ni preteklih dogodkov</button> </div> <?php endif; ?> Can this be done somehow? Thank you very much R Link to comment Share on other sites More sharing options...
zoeck Posted August 25, 2020 Share Posted August 25, 2020 Just use the field or Selector ? https://processwire.com/docs/selectors/#or-selectors2 <?php $events = $pages->find('template=calendar-post, Start_date|End_date>=today, sort=Start_date, limit=10'); ?> 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