1

Currently I have a Date/Time field that outputs in a Block View as:

Friday, April 25, 2014 - 4:00pm to 6:00pm

But i would like to output it as something like this:

Friday, April 25th from 4pm to 6pm

I was hoping that I would be able to do this by changing the way that the field outputs in the Rewrite Output option, but I did not find a way to get the the right machine names of the field I need to parse everything out as planned.

Does anybody have any other better ideas that would possibly work for this? Thank you!

1

Drupal 7 allows you to create custom date formats on your configuration page under "regional and language" "date and time." Once in admin/config/regional/date-time click the "Formats" tab. Then click "add format." You can now define custom dates using the variables available on the PHP date website. The drupal config page includes a link to the PHP date website as well.

I believe the output you are looking for would be: l, F dS, Y - ga (values from the PHP date website)

Once saved go back to the date and time config page and click "Add date type." Type in a name for your new display and select your new format.

In views you can now select the new custom date format you just created as your output.

  • Not that I am looking for an exact answer, but i am still a bit confused as far as what the format would be. Your output was Displayed as: Monday, January 27th 2014- 6pm. I am guessing that i wouldnt need the y for year, but how would i go about adding to and from in that string format? I also am still struggling with getting the start time 4pm to show up as you can see. – scapegoat17 Jan 27 '14 at 23:07
  • I was actually able to get the output to display as Monday, January 27th from to 6pm, but I still need that start time of 4pm in there. Any ideas? – scapegoat17 Jan 27 '14 at 23:11
  • Do you have a start and end date on your date field? If you have both you just need to tell views to display" both start and end dates" and you'd have a display of Monday, January 27th, 2014 - 4pm to 6pm. Views automatically places the word "to" inbetween the start and end time – pokermoneyclips Jan 27 '14 at 23:16
  • Once I added it, it came out to this: Friday, April 25th 4pm from to here is the string that i entered: l, F dS f\r\o\m\ \t\o ga any ideas where i went wrong? – scapegoat17 Jan 27 '14 at 23:26
  • Get rid of "f\r\o\m" and "t\o." If you have a start and end time in your date field views will automatically add the word "to." Adding the word "from" is a bit more of a pain. A very dirty way to add the word "from" would be to create multiple date formats: one for Friday, April 25th and one for 4pm. Then create a 3 date fields in views, one for day month day year, one displaying your start time only with the word "from" appended and the other with your end time only with the word "to" appended and placing them all inline. – pokermoneyclips Jan 27 '14 at 23:34
1

If you are using a view the best thing you can do is utilize what is already there.

Date formats /admin/config/regional/date-time/formats/add . After creating your Date formats you can assign them to a human readable name at /admin/config/regional/date-time

Here is the PHP Manual for formatting dates. play around with it. there is a live preview in drupal it's way easier then it seems.

  1. You can add a date format for Friday, April 25th, a format for 4pm and a date format for 6pm. The reason you need 3 date formats is because you cannot add phrases like from into a date format string.

  2. Now that you have 3 date formats you can add your date field 3 times in the view.

  3. Assign the first date value the date-format of Friday, April 25th, the second as 4pm and the last as 6pm.

  4. for the last two date fields, exclude them from display and move them above the first date field in the re-arrange fields.

  5. Rewrite the first date field to be:

[YOUR_date_field_1] . ' from ' . [YOUR_date_field_2] . ' to ' [YOUR_date_field_3]

  1. This will output your desired date field Friday, April 25th from 4pm to 6pm the PROPER WAY
0

What I ended up doing was manipulating the view field, by grabbing the .tpl.php file from the views/theme directory called views-view-field.tpl.php and then copied it into my theme directory as views-view-field--field-event-date.tpl.php.

There I changed the print $output; line and changed it to:

$stuff = explode(" - ",$output); // Array ( [0] => Friday, April 25th [1] => 4pm to 6pm )
print $stuff[0] . ' from ' . $stuff[1];

Unfortunately, I am not sure if this is the correct way of going about all of it, but it does seem to do the trick. I also do not see any immediate threat of hurting anything else because it is only changing that specific field in that view. I will not mark this answer as correct only because I do not believe this would be the correct Drupal Way of going about this, but it did work for me.

Please let me know if anyone has any better ideas. Or if anyone thinks that this is a suitable way. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.