OSTips – How to Create New Date & Time Formats in Drupal

How to Create New Date & Time Formats in Drupal

I don’t know about you, but as a site builder when I add a date field to a content type in Drupal, and I tell it I do NOT want the time just the date and then I go to a node, and I enter the date, and it shows me both the date AND the time… it kind of drives me crazy!

How do we fix it? In this video, I’m going to show you how to create new date & time formats in Drupal sites, so you don’t have to worry about getting a time and a date when you only want the date.  And, as a bonus, you’ll get it in the format that you want.

Let’s get started!

“Welcome to OSTips from OSTraining. My name is Rod Martin.

I’ve just added a date field to my article content type, and you’ll note I’ve set it to date only.

Well, you know the problem.  When I have a node that uses this field, it’s automatically going to show the time even though I did not ask for it, and I did not enter a time.

If I go ahead and manage display for that field, you’ll note that the format has the time. Click on the gear.   I’m really limited here as to what kind of format I can choose.  Let’s say I want the long date so: Tuesday June 23rd 2020, but without the time.  There’s no format for that!

Let’s take a look at how to change that.

  • head over to configuration / regional / language / date and time formats

You can either edit one of the top three, but I don’t want to choose that, or you can create a new format. There’s two ways to do this:

  1. I can start from scratch
  2. I can click Edit and copy an existing one

I am choosing option #2.

  • copy one that is almost what I need
  • create a new one: long date with no time
  • paste that string in

Now what’s giving me the time is H : I.  It’s worth looking at the PHP manual here, so you can see all of the different options. You can create a date and time stamp to reflect pretty much anything you want. For example:

  1. 12 hour format
  2. 24 hour format
  3. day /month /week
  4. ordinal suffix for a day

I’m gonna change this. I’m going to remove the dash and the time.

  • L is going to be the day
  • F is going to give me the month
  • j is going to give me the days of the month without the leading zeros
  • S after the J
  • Y is for the four-digit year

Let’s take a look. Your format is going to be:  Tuesday June 23rd 2020

Now all I have to do is update my display to structure content types article and manage display.

  • head over to my date (and my new date format with no time is there)
  • click update
  • click Save
  • look at the node

And there it is: Tuesday June 23rd 2020

Well that’s it! It was pretty simple. With just a little bit of PHP and some help from PHP net, you can build whatever date and time formats you need for your Drupal website.

Thanks for watching. This has been OStips from OS Training, and I’m Rod Martin.”


Author

  • Rod Martin

    Rod holds two masters degrees and has been training people how to do "things" for over 25 years. Originally from Australia, he grew up in Canada and now resides just outside Cincinnati, Ohio. He has worked in both the non-profit and for-profit worlds, in small companies and large corporations. His extensive open source experience includes WordPress, Joomla and Drupal and he really knows how to help you get the most out of the system you chose. Rod plays ice hockey a couple of times a week and rides his Goldwing motorcycle pretty much everywhere he can.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x