Home Forums Plugin Support WP User Frontend Pro Date field output

This topic is: resolved

This topic contains 4 replies, has 2 voices, and was last updated by  Tareq Hasan 5 years, 6 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #7989

    docandtee
    Participant
    Post count: 25

    Hello
    I would like to use the date fields selector in a custom post type to output a date range calendar.
    The idea is that the user would select a start date and an end date of when they would be available. I would then output this on their profile page in a nice jquery calendar. The only trouble is that all the calendars I have seen require the days months and years to be separated out and passed independently. So rather than outputting data=”DD/MM/YYYY” I would like to be able to output data-days=”DD” data-months=”MM” data-year=”YYYY”.
    Is this possible to output the dates like this? If not can anyone recommend a decent way of displaying a date range calendar that would work with this format?

    Many thanks

    D

    #7997

    docandtee
    Participant
    Post count: 25

    Hello again
    I've made a little progress since my original question – but not quite solved it.
    So I can output the dates how I want it but for some reason it only works for the first (from date). The second set of dates always come out as 1,1,1970 – which is weird. Can you only use one date picker on a post at a time? As I explained in my original post, I want to use two date picker fields to output a date range.

    Here is the code I'm using in my template:

    from day: <?php echo date( 'j', strtotime( get_post_meta( $post->ID, 'from_date', true ) ) ); ?>, from month: <?php echo date( 'n', strtotime( get_post_meta( $post->ID, 'from_date', true ) ) ); ?>, from year:  <?php echo date( 'Y', strtotime( get_post_meta( $post->ID, 'from_date', true ) ) ); ?><br />
    
    to day: <?php echo date( 'j', strtotime( get_post_meta( $post->ID, 'to_date', true ) ) ); ?>, to month: <?php echo date( 'n', strtotime( get_post_meta( $post->ID, 'to_date', true ) ) ); ?>, to year:  <?php echo date( 'Y', strtotime( get_post_meta( $post->ID, 'to_date', true ) ) ); ?> 

    And this is what gets output:

    from day: 9, from month: 12, from year: 2013
    to day: 1, to month: 1, to year: 1970

    Any ideas why the second date picker's results are wrong?

    Thanks

    D

    #8003

    Tareq Hasan
    Keymaster
    Post count: 2831

    I am not sure why is that happening, but most probably the value is getting wrong or in wrong format. Try saving the date value in year-month-day format.

    Also, calling 6 times the get_post_meta is not a good sign. You may try with this single line:
    [php]list($day, $month, $year) = explode( ‘-‘, date(‘j-n-Y', strtotime(get_post_meta( $post->ID, ‘from_date', true )) ) );
    var_dump($day, $month, $year);[/php]

    #8019

    docandtee
    Participant
    Post count: 25

    Hi Tareq
    Thanks for the reply.
    I couldn't get your code to work unfortunately. I did manage to get it working with the code below. But the thing that got rid of the weird 1970 date was to change the date format to yy-mm-dd. Does my code below look ok to you?

    <?php $from_date = get_post_meta($post->ID, 'from_date', true);?>
    <?php $fixed_from_date = strtotime($from_date); ?>
    <!-- Display Date -->
    day: <?php echo date('j' , $fixed_from_date) ; ?>
    month: <?php echo date('n' , $fixed_from_date) ; ?>
    year: <?php echo date('Y' , $fixed_from_date) ; ?>
    
    <?php $until_date = get_post_meta($post->ID, 'until_date', true);?>
    <?php $fixed_until_date = strtotime($until_date); ?>
    <!-- Display Date -->
    day: <?php echo date('j' , $fixed_until_date) ; ?>
    month: <?php echo date('n' , $fixed_until_date) ; ?>
    year: <?php echo date('Y' , $fixed_until_date) ; ?>
    #8020

    Tareq Hasan
    Keymaster
    Post count: 2831

    Yeah, it looks good now.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Date field output’ is closed to new replies.