Add line break after page attribute values when certain characters display

Permalink
Update:
I found an Add-on which will allow me to add multiple date and times against a page using attributes. I then used a Page Attribute Display to display this on my page. So this works and just need to work on the formatting of the output now.
My page currently displays:
2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM

What would I need php syntax-wise to get this to display as
2016-01-13 1:00 PM 3:00 PM
2016-01-14 1:00 PM 3:00 PM
2016-01-14 8:00 PM 10:00 PM?

So basically in pseudo code something that says
If :^: if found
add a new line break

And I suppose as it can be a whole heap of dates and times we probably need an array? You can probably tell I'm out of my depth now :)

Thanks!

----

Hi,
I'm no developer so really need your help! I'm creating a website for a small theatre. Every show has a range of show dates (not necessarily consecutive) and show times (several on one day).

I'm want to give the theatre director the ability to add show dates and show times to a "show" page using a date picker and then display this to the user (public) as non-editable text.

For example, I have a page for say Phantom of the Opera. On this page I have the usual image, content, etc. I want to also show the dates and the times on the page. So the user (general public) should just be able to view the dates and times (formatted as: l, d F g:ia) and not edit it.

The other option I tried was to create a custom date/time attribute and then display this on the page. This works well except I will have to create about 12 attributes as each can only hold one value eg Show time 1, Show time 2, Show time 3, etc. So unless there is a way that you can assign multiple values to one attribute (eg, "add another date/time), I'm not sure this will work?

Any help will be much appreciated as I'm out of ideas and under a bit of pressure.

Cheers!

Choppie
 
Choppie replied on at Permalink Reply
Choppie
Update:
I found an Add-on which will allow me to add multiple date and times against a page using attributes. I then used a Page Attribute Display to display this on my page. So this works and just need to work on the formatting of the output now.
My page currently displays:
2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM

What would I need php syntax-wise to get this to display as
2016-01-13 1:00 PM 3:00 PM
2016-01-14 1:00 PM 3:00 PM
2016-01-14 8:00 PM 10:00 PM?

So basically in pseudo code something that says
If :^: if found
add a new line break

And I suppose as it can be a whole heap of dates and times we probably need an array? You can probably tell I'm out of my depth now :)

Thanks!
MrKDilkington replied on at Permalink Reply 1 Attachment
MrKDilkington
Hi Choppie,

What add-on are you using?

Is this ":^:" character combination something you added or in the output?

Is this the output from one Page Attribute Display block?
"2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM"

If this is a string from one Page Attribute Display block, then you can use the attached zip file. The zip file contains a custom template.
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
echo $controller->getOpenTag();
echo "<span class=\"ccm-block-page-attribute-display-title\">".$controller->getTitle()."</span>";
$dates = $controller->getContent();
echo str_replace(':^:', '<br>', $dates);
echo $controller->getCloseTag();

- unzip the file into application\blocks
application\blocks\page_attribute_display\templates\date_break.php
- apply the Date Break custom template to the Page Attribute Display block
http://documentation.concrete5.org/editors/in-page-editing/block-ar...
Choppie replied on at Permalink Reply
Choppie
Hi MrKDilkington,

Thank you for your reply! I am using the ProEvents Calendar & Events Management add-on. The add-on has several page attributes added automatically. I have used the Events Dates attribute which allows me to pick a date and time from a "date/time picker". There is also a function to "Add Date" which allows you to add multiple dates and times.

I then used a Page Attribute Display to display these dates and times. But as there are more than one value, it concatenates the values with ":^:". I've also discovered that the values are displayed as a string and not a date/time. So not sure how I'm going to get the date formatting right - the native block date formatting (Page Attribute Display) won't work on a string (I think).

I will give what you've suggested a try and let you know how it goes.

Thank you very much for your help, it's very much appreciated!
MrKDilkington replied on at Permalink Reply
MrKDilkington
@Choppie

The following is an example of how you can format the date.
<?php defined('C5_EXECUTE') or die(_("Access Denied."));
echo $controller->getOpenTag();
echo "<span class=\"ccm-block-page-attribute-display-title\">".$controller->getTitle()."</span>";
// 2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM
$dateTimesString = $controller->getContent();
// split $dateTimesString into an array using ":^:" as the delimiter
$dateTimesArray = explode(':^:', $dateTimesString);
// array(
//   0 => '2016-01-13 1:00 PM 3:00 PM',
//   1 => '2016-01-14 1:00 PM 3:00 PM',
//   2 => '2016-01-14 8:00 PM 10:00 PM',
// )
// loop through the $dateTimesArray array
foreach ($dateTimesArray as $dateTimes) {
    // 2016-01-13 1:00 PM 3:00 PM

The comments should allow you to customize the date and times.

This link has additional date formatting options:
http://php.net/manual/en/function.date.php...
Choppie replied on at Permalink Reply
Choppie
Hi MrKDilkington,

Thank you SO MUCH for your help thus far! It works an absolute charm and was really easy to understand and apply.

One last question, I actually understand the code you have provided and can see that you feed the string from the attributes into an array, and then split the date and the time in the string by counting the characters and assigning it to variables. From there the date is formatted and concatenated to the time and displayed to the user.

I've managed to copy the logic in splitting the time into a "from" and a "to" part by adding
$timeFrom = substr($times, 0, 8);
$timeTo = substr($times, 8);
echo date('l, j F - ', strtotime($date)) . $timeFrom . ' to ' . $timeTo . '<br>';

so I could add "to" between the start and finish times (full code below). The problem with this is that somewhere in the add-on they must use a time format without leading zeros. I say this as some dates display as "Friday, 15 January - 3:45 AM to 5:45 AM" and some as
"Saturday, 16 January - 12:15 A to M 2:15 AM". This is because character 8 is in different places for the two examples.

Do you perhaps know in which of the proevents files I should change the g to an h to format the time correctly? I almost think that in our code it would be too late to do this and it needs to be done before we create our array - or is this not correct?

Thanks again,
Choppie

<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
echo $controller->getOpenTag();
echo "<span class=\"ccm-block-page-attribute-display-title\">".$controller->getTitle()."</span>";
// 2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM
$dateTimesString = $controller->getContent();
// split $dateTimesString into an array using ":^:" as the delimiter
$dateTimesArray = explode(':^:', $dateTimesString);
// array(
//   0 => '2016-01-13 1:00 PM 3:00 PM',
//   1 => '2016-01-14 1:00 PM 3:00 PM',
//   2 => '2016-01-14 8:00 PM 10:00 PM',
// )
// loop through the $dateTimesArray array
foreach ($dateTimesArray as $dateTimes) {
MrKDilkington replied on at Permalink Reply 1 Attachment
MrKDilkington
@Choppie

I have attached another custom template.
<?php defined('C5_EXECUTE') or die(_("Access Denied."));
echo $controller->getOpenTag();
echo "<span class=\"ccm-block-page-attribute-display-title\">".$controller->getTitle()."</span>";
?>
<style>
.date,
td.time:nth-child(2) {
    text-align: right;
}
.date {
    padding-right: 4px;
}
.time-separator {
    padding: 0px 5px;
}
Choppie replied on at Permalink Reply
Choppie
Hi MrKDilkington,

Thank you once again for the help and advice!!! It is really appreciated!

However, I now seem to get the following output:
Thursday, 14 January - 1:30 AM to 3:30 AM
Friday, 15 January - :45 AM to :45 AM

I think this is because the code says to grab the first position "after" the first 0 but should include the 0?

// if the time to starts with 0, grab the rest of the string after the 0
    if (strpos($timeTo, '0') == 0) {
        $timeTo = substr($timeTo, 1);
    }


I've tried changing the values above (1 to a 0, and the 0 to a -1) but this doesn't seem to make a difference. Just trying to guess by looking at the "pattern".

Sorry to be such a pain and total noob!
Cheers,
Choppie
MrKDilkington replied on at Permalink Reply 2 Attachments
MrKDilkington
@Choppie

You said that some of the times had a leading 0 and some did not, so this was the string I was testing with (an extra date/time with leading zeros was added).
2016-01-13 1:00 PM 3:00 PM:^:2016-01-14 1:00 PM 3:00 PM:^:2016-01-14 8:00 PM 10:00 PM:^:2016-02-14 08:00 AM 03:00 PM

In the first screenshot, one of the times has a leading zero. In the second screenshot, the leading zero is removed.

"I think this is because the code says to grab the first position "after" the first 0 but should include the 0?"

String positions start at 0 (not 1).

Example:
http://sandbox.onlinephpfunctions.com/code/18d26f84713a38e9dfce3d71...
Choppie replied on at Permalink Reply
Choppie
Hey,

I managed to fix this by changing the 1 to a 0 as follows:
if (strpos($timeFrom, '0') == 0) {
        $timeFrom = substr($timeFrom, 0);
    if (strpos($timeTo, '0') == 0) {
        $timeTo = substr($timeTo, 0);


Everything is now perfect!

Once again thank you very much for your help!!!

Cheers,
Choppie
Choppie replied on at Permalink Reply
Choppie
Hi MrKDilkington,

Me again... after all this time - I know!

I've noticed that my AM and PM's seem to be the wrong way round. What I mean is if I pick PM from the Event Dates attribute, it displays AM on the page, and vice versa.

I've looked at the code and can't seem to find the issue?

<table class="show-times-table">
<?php
$dateTimesString = $controller->getContent();
$dateTimesArray = explode(':^:', $dateTimesString);
 {
    $date = substr($dateTimes, 0, 10);
    $times = trim(substr($dateTimes, 10));
    $firstM = strpos($times, 'M');
    $timeFrom = substr($times, 0, $firstM + 1);
    if (strpos($timeFrom, '0') == 0) {
        $timeFrom = substr($timeFrom, 0);
    }
    $timeTo = substr($times, $firstM + 2);
    if (strpos($timeTo, '0') == 0) {
        $timeTo = substr($timeTo, 0);


Any idea if the problem is above or whether it's a bug of the Pro Events add-on?

Thanks,
Choppie
MrKDilkington replied on at Permalink Reply
MrKDilkington
@Choppie

If you comment out the code you added, so only the default string is displayed, are the AM and PM in the wrong position?

If not, then it could be an issue with the add-on. Which I would make a support issue for.