我很久以前就在尝试使用jquery插件了。这样,我想从自定义字段中的日期(2013/03/04格式)开始倒计时。Echo$Begin2告诉我:34天前。工作很好。
问题是我想把数字“34”作为一个数值。使用以下代码,echo $Nombre
给我“0”。我想是因为get_post_meta($post->ID, \'Begin\', true);
不是数值吗?
$Begin = get_post_meta($post->ID, \'Begin\', true);
$Begin2 = \'<abbr class="timeago" title="\'.$Begin.\'">\'.$Begin.\'</abbr>\';
echo $Begin2;
$Nombre = (int) substr($Begin, 0, strpos($Begin, \' \'));
echo $Nombre;
SO网友:Chris_O
// Correct way to convert string to integer
$begin = \'34\';
$number = (int)$begin;
var_dump( $number );
int 34
//Another correct way to convert string to integer
$number = (int)get_post_meta( get_the_ID(), \'begin\', true );
var_dump( $number );
int 34
//strops() = Find the numeric position of the first occurrence of needle in the haystack string.
// returns the numeric position or false if not found
$var = strpos( $begin, \' \');
var_dump( $var );
boolean false
//substr() = Returns the portion of string specified by the start and length parameters.
//Your passing 0 as start and since false gets interpreted as 0 your passing 0 as length
$substring = substr( $begin, 0, false );
var_dump( $substring );
string \' \'
//Same thing here but your converting the returned empty string into an integer which returns 0
$substring = (int)substr( $begin, 0, false );
var_dump( $substring );
int 0