September 10, 2013 at 6:22 pm 7989 |
September 10, 2013 at 8:25 pm 7997 |
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
| 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
| 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 |