WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use the jquery plugin Timeago. With this, I want to countdown from a date (2013/03/04 format) Which is in a custom field. Echo $Begin2 gives me something like : 34 days ago. It's work fine.

The problem is I want to take the number "34" as a numerical value. With the following code, echo $Nombre gives me "0". I think it's because get_post_meta($post->ID, 'Begin', true); is not a numerical value ?

   $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;
share|improve this question
// 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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.