1

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;
2
// 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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