[Drupal] How do I format simple PHP date field inside EFQ

Hi all

Having a small issue trying to display my date field in a certain way inside my EFQ.
I setup a event content type and set DATE format, worked very well using views but defaults to something else in EFQ.

VIEW - 15 March, 2014 - 22:00
EFQ - 2014-03-15 22:00:00

How do I change it so my EFQ prints the date like it does in my view?
Just not sure what PHP code to add or where to add it?

'#markup'=> '<strong>When:</strong> ' . $node->field_event_date_and_time['und'][0]['value'],

The above snippet is part of my EFQ which displays the date and time 2014-03-15 22:00:00

Any ideas how to change it?

Thanks, cb

I need to add something like

strtoupper(date("l j g:ia"))

I also tried using the custom approach but nothing changed, not even an error

$node->field_event_date_and_time['und'][0]['value'], 'custom', 'F a'

Any thoughts?
I’m also faced with a few other issues and wondering exactly where does all the logic go inside a EFQ?

Thanks, cb

You’re going to have to use datetime format or something like that. Look at the example from Craig Constable on this page: http://www.php.net/manual/en/datetime.format.php

Thanks awasson

I was looking at this and lots of other handy PHP functions though I 'm not sure how I add this to the code.
Just wondering how I would tie this in with the output, the field_event_date_and_time field ?



Thanks oddz

What I can gather, I just need to add date(“F j, Y, g:i a”), but how?
Where does it all the code go? Inside my eventlist.module?

This is what I can’t understand.

For instance
function format_date… or $formatted_date = format_date(…

Do I add it inside the foreach loop, outside the eventlist_block_eventlist() function?
Create a new file? Add it inside template.php ?


A wild guess, but I think wrapping
with a strtotime to get it into a timestamp,
then wrapping that with date to format it would work.

Thanks Mittineague

Something like this though gives an error?

'#markup'=> '<strong>When:</strong> ' . strtotime(date("l j g:ia"), $node->field_event_date_and_time['und'][0]['value']),

Notice: A non well formed numeric value encountered in eventlist_block_eventlist() (line 88 of…


Just so things are clear

The correct date format is already setup inside my event content type and all my event nodes display the correct date format.
I also have the Date and time configured inside my main config - Long Medium Short to just how I need it.

It works everywhere beside in the EFQ.

Nesting is a PITA isn’t it? What I had in mind was

'#markup'=> '<strong>When:</strong> ' . date("l j g:ia", strtotime($node->field_event_date_and_time['und'][0]['value'])),

It works! :smiley:

Nesting is a PITA isn’t it?

PITA? First time I’ve heard of this.

And thanks Mittineague - I’m now ready for the next step, next challenge.


What Mittineague has suggested is the wrong way to do this in Drupal. In Drupal you essentially have a list of date types that map to any type of display. The advantage of this is if the display needs to change you just change the configuration for a single date type and it changes it everywhere. While what is suggested works it is wrong in terms of how things *should be done in Drupal. With that said, what is the name of your date format?

The code would go in your module, something ike this:

'#markup'=> '<strong>When:</strong> ' . date_format($node->field_event_date_and_time['und'][0]['value'],'<machine_name_of_your_date_format>'),

As simple as calling a function.

Sorry if I’ve misled. I thought computerbarry had tried the Drupal way but for some reason it didn’t work with EFQ and needed to use “raw” PHP.

Off Topic:

PITA - pain in the *** i.e. in this case, messy, confusing, not elegant, and most likely not the best way to go about it.(maintenance problems)

I see what you mean oddz and makes sense that we already have the date type configured.
So would this work in a similar way to a CSS class, something we can use over and over wherever its needed?

I setup a date format - EventDate, so…

'#markup'=> '<strong>When:</strong> ' . date_format($node->field_event_date_and_time['und'][0]['value'],'EventDate'),

Warning: date_format() expects parameter 1 to be DateTime, string given in eventlist_block_eventlist() (line 89 of

I also noticed that the date types all have month, date, year, time etc. (it doesn’t give you the option to just select one)
What if I only want to show the time?

Thanks, Barry


I thought computerbarry had tried the Drupal way but for some reason it didn’t work with EFQ and needed to use “raw” PHP

Thats exactly right Mittineague.
And with your help we’ve realised what the correct way is while learning some handy PHP also.

PITA - pain in the *** i.e. in this case, messy, confusing, not elegant, and most likely not the best way to go about it.(maintenance problems)

Thanks for clearing that up :cool:

Got the name wrong. The name of the function is format_date. The date_format function is native php date formatting.

'#markup'=> '<strong>When:</strong> ' . format_date($node->field_event_date_and_time['und'][0]['value'],'EventDate'),

Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() (line 2014 of /home2/…/includes/common.inc).


line 2014 from core common.inc

// Set the time zone for the DateTime object.
date_timezone_set($date_time, $timezones[$timezone]);


Yeah as far as I can tell EFQ isn’t going let you set up dates as usual in Drupal via date formats in the Date/Time settings.

Barry, where is this markup from?

'#markup'=> '<strong>When:</strong> ' . $node->field_event_date_and_time['und'][0]['value'],  

That is the code from your first post that resulted in: 2014-03-15 22:00:00

My suggestion would be to modify that as follows:


$date = date_create($node->field_event_date_and_time['und'][0]['value']);
$formatted_date =  date_format($date, 'jS F, Y - g:ia');
'#markup'=> '<strong>When:</strong> ' . $formatted_date . 'the rest of your code';  


Thanks all

I’ve just tried it awasson and it works.

foreach($nodes as $node) {
    $date = date_create($node->field_event_date_and_time['und'][0]['value']);
    $formatted_date =  date_format($date, 'g:ia');
    $build['node_'.$node->nid]['field_event_date_and_time'] = array(
      '#type'=> 'markup',
      '#markup'=> '<strong>When:</strong> ' . $formatted_date,
      '#prefix'=> '<div>', 
      '#suffix'=> '</div>',
      '#weight'=> 1,

I’d say what Mittineague recommended works better - less code and does the same job. Though its nice to see a couple of possible solutions :slight_smile:

'#markup'=> '<strong>When:</strong> ' . date("g:ia", strtotime($node->field_event_date_and_time['und'][0]['value'])),

And as oddz pointed out this is not the correct way to do things in Drupal and a date type should be used.

'#markup'=> '<strong>When:</strong> ' . format_date($node->field_event_date_and_time['und'][0]['value'],'EventDate'),

As things stand, calling the date type doesn’t work as outlined in post #15 above and I’m not sure how to just select the time from this date type because want I can see date types don’t allow you to create a time only. This is another problem, if I get oddz solution to work, how do I select only the time? Only the month etc.


Yes, I agree Mittineague’s suggestion to use strtotime is the purest solution. strtotime() + date() were a favorite combination when I used to do a lot of straight PHP date manipulation.