[英]Add one day to get_the_date in Wordpress
在 WPTouch 主題中,發布日期顯示在一個大氣泡中。 但是,帖子是前一天晚上添加的,所以一天落后了一天。 為了解決這個問題,我改變了這一行:
<?php wptouch_the_time( 'j' ); ?>
對此:
<?php
$date = DateTime::createFromFormat('m-d-Y', mysql2date('m-d-Y', $post->post_date));
$date->modify('+1 day');
echo $date->format('j');
?>
這有效,但非常丑陋。 我認為應該有更好的方法從 mysql 日期到 php DateTime。
您可以使用此過濾器在使用get_the_date()
函數( wptouch_the_time()
最有可能用於獲取日期)將日期賦予主題之前修改日期:
add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
function modify_get_the_date( $value, $format, $post ) {
$date = new DateTime( $post->post_date );
$date->modify( "+1 day" );
if ( $format == "" )
$format = get_option( "date_format" );
return( $date->format( $format ) );
}
話雖如此,與其修改從數據庫中讀取的日期,不如僅在寫入時存儲發布日期。 您可以通過安排第二天的帖子來實現這一點,而不是直接發布它們。
你可以這樣做
<?php
$date=date_create();
date_modify($date,"+1 month");
echo date_format($date,"Y-m-d");
?>
我總是這樣做的方式是使用strtotime
方法
<?php
$format = ""; // your date format
$oneDayLater = strtotime("+1 day", strtotime($columnValue));
$out = date($format, $oneDayLater);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.