SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [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?

    PHP Code:
    '#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
    The more you learn.... the more you learn there is more to learn.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to add something like
    PHP Code:
    strtoupper(date("l j g:ia")) 
    I also tried using the custom approach but nothing changed, not even an error
    PHP Code:
    $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
    The more you learn.... the more you learn there is more to learn.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,028
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?

    Barry
    The more you learn.... the more you learn there is more to learn.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The only code I hate more than my own is everyone else's.

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?

    Thanks
    The more you learn.... the more you learn there is more to learn.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,995
    Mentioned
    186 Post(s)
    Tagged
    2 Thread(s)
    A wild guess, but I think wrapping
    $node->field_event_date_and_time['und'][0]['value']
    with a strtotime to get it into a timestamp,
    then wrapping that with date to format it would work.

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mittineague

    Something like this though gives an error?
    PHP Code:
    '#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...
    Barry


    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.
    The more you learn.... the more you learn there is more to learn.

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,995
    Mentioned
    186 Post(s)
    Tagged
    2 Thread(s)
    Nesting is a PITA isn't it? What I had in mind was
    PHP Code:
    '#markup'=> '<strong>When:</strong> ' date("l j g:ia"strtotime($node->field_event_date_and_time['und'][0]['value'])), 

  10. #10
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works!

    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.

    Barry
    The more you learn.... the more you learn there is more to learn.

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    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:

    PHP Code:
    '#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.
    The only code I hate more than my own is everyone else's.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,995
    Mentioned
    186 Post(s)
    Tagged
    2 Thread(s)
    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)

  13. #13
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

    PHP Code:
    '#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


    UPDATE
    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
    The more you learn.... the more you learn there is more to learn.

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,135
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Got the name wrong. The name of the function is format_date. The date_format function is native php date formatting.

    PHP Code:
    '#markup'=> '<strong>When:</strong> ' format_date($node->field_event_date_and_time['und'][0]['value'],'EventDate'), 
    The only code I hate more than my own is everyone else's.

  15. #15
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
    // Set the time zone for the DateTime object.
    date_timezone_set($date_time$timezones[$timezone]); 
    Thanks
    The more you learn.... the more you learn there is more to learn.

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,028
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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?
    PHP Code:
    '#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:

    PHP Code:

    <?php

    $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';  

    ?>
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  17. #17
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all

    I've just tried it awasson and it works.

    PHP Code:
    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

    PHP Code:
    '#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.

    PHP Code:
    '#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.

    Barry
    The more you learn.... the more you learn there is more to learn.

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,028
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •