Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;

Thank you for your help !

share|improve this question
add comment

1 Answer

// 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
add comment

Your Answer

 
discard

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.