最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Unable to format dates to put in event calendar

matteradmin8PV0评论

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.

[{"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"}]

Share Improve this question asked Mar 25, 2019 at 13:01 Zaheer AbbasZaheer Abbas 968 bronze badges 2
  • 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
Add a comment  | 

1 Answer 1

Reset to default 1

I 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.

Post a comment

comment list (0)

  1. No comments so far