Date field output

This topic contains 4 reply and 2 voices, and was last updated by Tareq Hasan 10 years, 7 months ago
Viewing 4 Posts - 1 through 4 (of 4 total)
Author Posts
September 10, 2013 at 6:22 pm 7989
Tareq Hasan 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
September 10, 2013 at 8:25 pm 7997
docandtee docandtee

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

September 11, 2013 at 9:09 am 8003
Tareq Hasan Tareq Hasan

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]

September 11, 2013 at 3:59 pm 8019
docandtee docandtee

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) ; ?>
September 11, 2013 at 4:18 pm 8020
Tareq Hasan Tareq Hasan

Yeah, it looks good now.

Viewing 4 Posts - 1 through 4 (of 4 total)