substr not working? php


substr not working? php

不幸的是,我无法为您提供一个实际的示例。但我有一个表单,它向数据库提交了一堆数据。

我用检索"日期"字段

genesis_custom_field('date')

输出日期为"mm/dd/yyyy",例如2012年2月19日

不应该

echo substr(genesis_custom_field('date'), 0, 2) . '<-M<br />'

输出"02"

相反,它输出的是完整的日期格式"02/19/2012"。

意图和目的

出于意图和目的,以下是代码示例。

// The Query
$reviews_query = new WP_Query('showposts=1');
// The Loop
while ( $reviews_query->have_posts() ) : $reviews_query->the_post();
echo substr(genesis_custom_field('date'), 0, 2) . '<-M<br />';
endwhile;
// Reset Post Data
wp_reset_postdata();

具有的预期输出

02<-M

但我得到

02/19/2012<-M

函数genesis_custom_field实际上会回显该值,而不是返回该值,这就是substr不起作用的原因。您基本上是在一个null值上调用substr

如果您想要返回值以便修改它,请使用函数genesis_get_custom_field