admin管理员组文章数量:1026989
What i want to achieve is to get dates through get_post_meta and push them in array and passing it into js code. But when i use below code first post in loop's dates gets formatted and other posts dates showing 01-01-1970. Working since 5 hours any help will be appreciated.
Here is wordpress the code.
<?php
function bpem_event_calendar_tab() {
global $bp;
if (isset($bp->groups->current_group->slug)) {
bp_core_new_subnav_item(array(
'name' => 'Event Calendar',
'slug' => 'events-calendar',
'parent_slug' => $bp->groups->current_group->slug,
'parent_url' => bp_get_group_permalink($bp->groups->current_group),
'screen_function' => 'bpem_event_calendar_roup_show_screen',
'position' => 90));
}
}
add_action('wp', 'bpem_event_calendar_tab');
function bpem_event_calendar_roup_show_screen() {
//add_action('bp_template_title', 'ev_new_group_show_screen_title');
add_action('bp_template_content', 'bpem_event_calendar_group_show_screen_content');
$templates = array('groups/single/plugins.php', 'plugin-template.php');
if (strstr(locate_template($templates), 'groups/single/plugins.php')) {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'groups/single/plugins'));
} else {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'plugin-template'));
}
}
/*function ev_new_group_show_screen_title() {
echo 'New Tab Title';
}*/
function bpem_event_calendar_group_show_screen_content() {
$group_name = sanitize_title(bp_get_current_group_name());
$event_data = array();
$args = array(
'post_type' => 'bpem_event',
'posts_per_page' => -1,
/* 'meta_key' => 'evn_group_slug',
'meta_value' => $group_name,*/
);
$i=0;
$event_query = new WP_Query( $args );
if ( $event_query->have_posts() ) : while ( $event_query->have_posts() ) :
$event_query->the_post();
$start_date = get_post_meta( get_the_id(), 'evn_startDate');
//print_r($start_date);
$start_d = date("Y-m-d", strtotime($start_date[$i]));
//echo $start_d.'<br>';
$start_time = get_post_meta(get_the_id(), 'evn_startTime');
$start_t = date("H:i:s", strtotime($start_time[$i]));
$end_date = get_post_meta( get_the_id(), 'evn_endDate');
//print_r($end_date);
$end_d = date("Y-m-d", strtotime($end_date[$i]));
//echo $end_d;
$end_time = get_post_meta(get_the_id(), 'evn_endTime');
$end_t = date("H:i:s", strtotime($end_time[$i]));
$event_data[] = array(
'title' => get_the_title(),
'start' => $start_d.'T'.$start_t,
'end' => $end_d.'T'.$end_t
//'end_date' => $enddate[0]
);
$i++;
endwhile;
wp_reset_postdata();
endif;
$json = json_encode($event_data);
print_r($json);
echo "<div id='bpem-calendar'></div>";
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var todayDate = jQuery.datepicker.formatDate('yy-mm-dd', new Date());
jQuery('#bpem-calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: todayDate,
businessHours: true, // display business hours
editable: false,
events:<?php echo json_encode($event_data); ?>
});
});
</script>
<?php
} ?>
This is the output.
[{"title":"JSON Expert Meetup","start":"2019-03-25T07:00:00","end":"2019-03-27T09:30:00"},{"title":"WordPress MeetUp Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"WP Meetup Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"JQuery Masters Meetup","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"}]
What i want to achieve is to get dates through get_post_meta and push them in array and passing it into js code. But when i use below code first post in loop's dates gets formatted and other posts dates showing 01-01-1970. Working since 5 hours any help will be appreciated.
Here is wordpress the code.
<?php
function bpem_event_calendar_tab() {
global $bp;
if (isset($bp->groups->current_group->slug)) {
bp_core_new_subnav_item(array(
'name' => 'Event Calendar',
'slug' => 'events-calendar',
'parent_slug' => $bp->groups->current_group->slug,
'parent_url' => bp_get_group_permalink($bp->groups->current_group),
'screen_function' => 'bpem_event_calendar_roup_show_screen',
'position' => 90));
}
}
add_action('wp', 'bpem_event_calendar_tab');
function bpem_event_calendar_roup_show_screen() {
//add_action('bp_template_title', 'ev_new_group_show_screen_title');
add_action('bp_template_content', 'bpem_event_calendar_group_show_screen_content');
$templates = array('groups/single/plugins.php', 'plugin-template.php');
if (strstr(locate_template($templates), 'groups/single/plugins.php')) {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'groups/single/plugins'));
} else {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'plugin-template'));
}
}
/*function ev_new_group_show_screen_title() {
echo 'New Tab Title';
}*/
function bpem_event_calendar_group_show_screen_content() {
$group_name = sanitize_title(bp_get_current_group_name());
$event_data = array();
$args = array(
'post_type' => 'bpem_event',
'posts_per_page' => -1,
/* 'meta_key' => 'evn_group_slug',
'meta_value' => $group_name,*/
);
$i=0;
$event_query = new WP_Query( $args );
if ( $event_query->have_posts() ) : while ( $event_query->have_posts() ) :
$event_query->the_post();
$start_date = get_post_meta( get_the_id(), 'evn_startDate');
//print_r($start_date);
$start_d = date("Y-m-d", strtotime($start_date[$i]));
//echo $start_d.'<br>';
$start_time = get_post_meta(get_the_id(), 'evn_startTime');
$start_t = date("H:i:s", strtotime($start_time[$i]));
$end_date = get_post_meta( get_the_id(), 'evn_endDate');
//print_r($end_date);
$end_d = date("Y-m-d", strtotime($end_date[$i]));
//echo $end_d;
$end_time = get_post_meta(get_the_id(), 'evn_endTime');
$end_t = date("H:i:s", strtotime($end_time[$i]));
$event_data[] = array(
'title' => get_the_title(),
'start' => $start_d.'T'.$start_t,
'end' => $end_d.'T'.$end_t
//'end_date' => $enddate[0]
);
$i++;
endwhile;
wp_reset_postdata();
endif;
$json = json_encode($event_data);
print_r($json);
echo "<div id='bpem-calendar'></div>";
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var todayDate = jQuery.datepicker.formatDate('yy-mm-dd', new Date());
jQuery('#bpem-calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: todayDate,
businessHours: true, // display business hours
editable: false,
events:<?php echo json_encode($event_data); ?>
});
});
</script>
<?php
} ?>
This is the output.
Share Improve this question asked Mar 25, 2019 at 13:01 Zaheer AbbasZaheer Abbas 968 bronze badges 2 |[{"title":"JSON Expert Meetup","start":"2019-03-25T07:00:00","end":"2019-03-27T09:30:00"},{"title":"WordPress MeetUp Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"WP Meetup Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"JQuery Masters Meetup","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"}]
1 Answer
Reset to default 1I don't think you want $start_date[$i]
. You probably are looking for $start_date[0]
. That's why you're only getting the first result and none of the following results. 1970-01-01T00:00:00
is the epoch time and shows up because strtotime()
cannot convert the date you're giving it, so that's what it returns by default.
I would check out what $start_date
looks like.
What i want to achieve is to get dates through get_post_meta and push them in array and passing it into js code. But when i use below code first post in loop's dates gets formatted and other posts dates showing 01-01-1970. Working since 5 hours any help will be appreciated.
Here is wordpress the code.
<?php
function bpem_event_calendar_tab() {
global $bp;
if (isset($bp->groups->current_group->slug)) {
bp_core_new_subnav_item(array(
'name' => 'Event Calendar',
'slug' => 'events-calendar',
'parent_slug' => $bp->groups->current_group->slug,
'parent_url' => bp_get_group_permalink($bp->groups->current_group),
'screen_function' => 'bpem_event_calendar_roup_show_screen',
'position' => 90));
}
}
add_action('wp', 'bpem_event_calendar_tab');
function bpem_event_calendar_roup_show_screen() {
//add_action('bp_template_title', 'ev_new_group_show_screen_title');
add_action('bp_template_content', 'bpem_event_calendar_group_show_screen_content');
$templates = array('groups/single/plugins.php', 'plugin-template.php');
if (strstr(locate_template($templates), 'groups/single/plugins.php')) {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'groups/single/plugins'));
} else {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'plugin-template'));
}
}
/*function ev_new_group_show_screen_title() {
echo 'New Tab Title';
}*/
function bpem_event_calendar_group_show_screen_content() {
$group_name = sanitize_title(bp_get_current_group_name());
$event_data = array();
$args = array(
'post_type' => 'bpem_event',
'posts_per_page' => -1,
/* 'meta_key' => 'evn_group_slug',
'meta_value' => $group_name,*/
);
$i=0;
$event_query = new WP_Query( $args );
if ( $event_query->have_posts() ) : while ( $event_query->have_posts() ) :
$event_query->the_post();
$start_date = get_post_meta( get_the_id(), 'evn_startDate');
//print_r($start_date);
$start_d = date("Y-m-d", strtotime($start_date[$i]));
//echo $start_d.'<br>';
$start_time = get_post_meta(get_the_id(), 'evn_startTime');
$start_t = date("H:i:s", strtotime($start_time[$i]));
$end_date = get_post_meta( get_the_id(), 'evn_endDate');
//print_r($end_date);
$end_d = date("Y-m-d", strtotime($end_date[$i]));
//echo $end_d;
$end_time = get_post_meta(get_the_id(), 'evn_endTime');
$end_t = date("H:i:s", strtotime($end_time[$i]));
$event_data[] = array(
'title' => get_the_title(),
'start' => $start_d.'T'.$start_t,
'end' => $end_d.'T'.$end_t
//'end_date' => $enddate[0]
);
$i++;
endwhile;
wp_reset_postdata();
endif;
$json = json_encode($event_data);
print_r($json);
echo "<div id='bpem-calendar'></div>";
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var todayDate = jQuery.datepicker.formatDate('yy-mm-dd', new Date());
jQuery('#bpem-calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: todayDate,
businessHours: true, // display business hours
editable: false,
events:<?php echo json_encode($event_data); ?>
});
});
</script>
<?php
} ?>
This is the output.
[{"title":"JSON Expert Meetup","start":"2019-03-25T07:00:00","end":"2019-03-27T09:30:00"},{"title":"WordPress MeetUp Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"WP Meetup Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"JQuery Masters Meetup","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"}]
What i want to achieve is to get dates through get_post_meta and push them in array and passing it into js code. But when i use below code first post in loop's dates gets formatted and other posts dates showing 01-01-1970. Working since 5 hours any help will be appreciated.
Here is wordpress the code.
<?php
function bpem_event_calendar_tab() {
global $bp;
if (isset($bp->groups->current_group->slug)) {
bp_core_new_subnav_item(array(
'name' => 'Event Calendar',
'slug' => 'events-calendar',
'parent_slug' => $bp->groups->current_group->slug,
'parent_url' => bp_get_group_permalink($bp->groups->current_group),
'screen_function' => 'bpem_event_calendar_roup_show_screen',
'position' => 90));
}
}
add_action('wp', 'bpem_event_calendar_tab');
function bpem_event_calendar_roup_show_screen() {
//add_action('bp_template_title', 'ev_new_group_show_screen_title');
add_action('bp_template_content', 'bpem_event_calendar_group_show_screen_content');
$templates = array('groups/single/plugins.php', 'plugin-template.php');
if (strstr(locate_template($templates), 'groups/single/plugins.php')) {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'groups/single/plugins'));
} else {
bp_core_load_template(apply_filters('bp_core_template_plugin', 'plugin-template'));
}
}
/*function ev_new_group_show_screen_title() {
echo 'New Tab Title';
}*/
function bpem_event_calendar_group_show_screen_content() {
$group_name = sanitize_title(bp_get_current_group_name());
$event_data = array();
$args = array(
'post_type' => 'bpem_event',
'posts_per_page' => -1,
/* 'meta_key' => 'evn_group_slug',
'meta_value' => $group_name,*/
);
$i=0;
$event_query = new WP_Query( $args );
if ( $event_query->have_posts() ) : while ( $event_query->have_posts() ) :
$event_query->the_post();
$start_date = get_post_meta( get_the_id(), 'evn_startDate');
//print_r($start_date);
$start_d = date("Y-m-d", strtotime($start_date[$i]));
//echo $start_d.'<br>';
$start_time = get_post_meta(get_the_id(), 'evn_startTime');
$start_t = date("H:i:s", strtotime($start_time[$i]));
$end_date = get_post_meta( get_the_id(), 'evn_endDate');
//print_r($end_date);
$end_d = date("Y-m-d", strtotime($end_date[$i]));
//echo $end_d;
$end_time = get_post_meta(get_the_id(), 'evn_endTime');
$end_t = date("H:i:s", strtotime($end_time[$i]));
$event_data[] = array(
'title' => get_the_title(),
'start' => $start_d.'T'.$start_t,
'end' => $end_d.'T'.$end_t
//'end_date' => $enddate[0]
);
$i++;
endwhile;
wp_reset_postdata();
endif;
$json = json_encode($event_data);
print_r($json);
echo "<div id='bpem-calendar'></div>";
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var todayDate = jQuery.datepicker.formatDate('yy-mm-dd', new Date());
jQuery('#bpem-calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: todayDate,
businessHours: true, // display business hours
editable: false,
events:<?php echo json_encode($event_data); ?>
});
});
</script>
<?php
} ?>
This is the output.
Share Improve this question asked Mar 25, 2019 at 13:01 Zaheer AbbasZaheer Abbas 968 bronze badges 2[{"title":"JSON Expert Meetup","start":"2019-03-25T07:00:00","end":"2019-03-27T09:30:00"},{"title":"WordPress MeetUp Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"WP Meetup Karachi","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"},{"title":"JQuery Masters Meetup","start":"1970-01-01T00:00:00","end":"1970-01-01T00:00:00"}]
-
I don't think you want
$start_date[$i]
. You probably are looking for$start_date[0]
. I would check out what$start_date
looks like. – MikeNGarrett Commented Mar 25, 2019 at 13:07 - Bundle of thanks mike problem solved and yes that was the issue thumbs up. – Zaheer Abbas Commented Mar 25, 2019 at 13:18
1 Answer
Reset to default 1I don't think you want $start_date[$i]
. You probably are looking for $start_date[0]
. That's why you're only getting the first result and none of the following results. 1970-01-01T00:00:00
is the epoch time and shows up because strtotime()
cannot convert the date you're giving it, so that's what it returns by default.
I would check out what $start_date
looks like.
本文标签: custom post typesUnable to format dates to put in event calendar
版权声明:本文标题:custom post types - Unable to format dates to put in event calendar 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660900a2161885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$start_date[$i]
. You probably are looking for$start_date[0]
. I would check out what$start_date
looks like. – MikeNGarrett Commented Mar 25, 2019 at 13:07