PHP Wordpress日历跳过二月
使用Date-Time Piker插件的高级自定义字段,我用年、日期、时间am和pm的信息填充日历 在日历列表页面上,我有一个查询,出于某种原因,它跳过了二月,重复了三月两次 代码如下:PHP Wordpress日历跳过二月,php,jquery,Php,Jquery,使用Date-Time Piker插件的高级自定义字段,我用年、日期、时间am和pm的信息填充日历 在日历列表页面上,我有一个查询,出于某种原因,它跳过了二月,重复了三月两次 代码如下: <?php $today = date('Ymd h:i:s a', time() - 60 * 60 * 24); #start from current month. Change 2 to however months ahe
<?php
$today = date('Ymd h:i:s a', time() - 60 * 60 * 24);
#start from current month. Change 2 to however months ahead you want
for ($x=0; $x<=6; $x++) {
$date = new DateTime("$x months");
$date->modify("-" . ($date->format('j')-1) . " days");
#echo $date->format('j, m Y');
$month = $date->format('m');
$year = $date->format('Y');
#echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
WHERE meta_key LIKE %s
AND meta_value LIKE %s
ORDER BY meta_value ASC
",
'show_date_time_%_show_date', // meta_name: $ParentName_$RowNumber_$ChildName
#''.$year.''.$month.'%' // meta_value: 20131031 for example
''.$year.''.$month.'%' // meta_value: 20131031 for example
));
// loop through the results
if( $rows ) {
echo '<div class="month">';
echo '<h2>'.$date->format('F').' '.$date->format('Y').'</h2>';
echo '<ul>';
foreach( $rows as $row ) {
// for each result, find the 'repeater row number' and use it to load the sub field!
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'show_date_time_' . $matches[0] . '_show_date'; // $matches[0] contains the row number!
// today or later
$showDate = $row->meta_value;
$do_current = ($showDate > $today);
if ( $do_current || $continue ) :
$show_title = get_the_title( $row->post_id );
$machine_name = preg_replace('@[^a-z0-9-]+@','-', strtolower($show_title));
$post_type = get_post_type($row->post_id);
if( $post_type === 'pre-post-show' ){
// echo 'pre-post-show matching';
$postID = $row->post_id;
$posts = get_field('pre_post_related_show', $postID);
if( $posts ){
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$related_show = get_the_title($post->ID);
$related_show_machine = preg_replace('@[^a-z0-9-]+@','-', strtolower($related_show));
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}
}// post type define
?>
有没有想过为什么会发生这种情况?嗯,这比看起来要复杂一些。在这个stackoverflow中,您将找到这种奇怪行为的完整解释。总而言之: +1个月将原来的月数增加1。这使得日期为2015-02-31。 第二个月的2月在2015年只有28天,所以PHP自动更正了这一点,从2月1日开始继续计算天数。然后在3月3日结束。 这就是为什么你要看第三个月两次。如何避开这个问题?嗯,在我给你看的帖子中,有一些方法可以解决这个问题。如果您有php 5.3或更高版本,我建议:
$date = new DateTime();// Now.
for ($x=0; $x<=6; $x++) {
($x) ? $date->modify( 'first day of next month' ) : NULL;// It should give you next month from $x=0 onwards. Output: 01,02,03,04,05,06,07
....// Your code comes here
希望能有帮助
更新
有很多关于日期和PHP的文档。这门课处理日期很方便。检查其方法以更改日期。最后,看看不同的日期和时间。如果您对使用诸如“+7天”之类的字符串修改日期感兴趣,请特别注意
最后,看一下文档。wpdb类允许您与wp中的数据库进行交互。为了进行查询,您必须学习一些SQL。互联网上有数百页,更不用说帮助你学习的书籍了
例如,如果要检索未来7天内的所有帖子,可以执行以下操作:
$now_date = (new DateTime())->format("Y-m-d");
$next_seven_days = (new DateTime("+7 day"))->format("Y-m-d");
$query = "SELECT * FROM wp_posts WHERE post_date >= '$now_date' AND post_date < '$next_seven_days'";
$rows = $wpdb->get_results( $query, OBJECT );
注意:因为没有来自用户/访问者的输入,他们也不能影响查询,为了简单起见,我不打算使用prepare。无论如何,我必须说,系统地使用它是一种很好的做法。谢谢!这是非常有用的。是否有任何资源可以让我了解有关创建日期范围拉取的更多信息?我是从一位同事那里得到这个消息的,我想知道我该怎么说,例如:在接下来的7天内删除所有帖子。当然,没问题,请检查更新,如果它解决了您的问题,请接受答案,非常感谢。