Checking if a string is a timestamp

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I have a certain question here... how can I check if a string is a timestamp?

Like I got the following function going.
PHP Code: [ Select ]
<?php
   function get_age($date)
   {
      return date("Y") - date("Y", strtotime($date));
   }
?>
  1. <?php
  2.    function get_age($date)
  3.    {
  4.       return date("Y") - date("Y", strtotime($date));
  5.    }
  6. ?>

And I want the $date to be able to be used as a timestamp or a date as a string... if I run the timestamp into a strtotime... it doesn't give me an accurate answer... like the following test...
PHP Code: [ Select ]
<?php
   $tmsp = time();
   echo date('Y', $tmsp);
?>
  1. <?php
  2.    $tmsp = time();
  3.    echo date('Y', $tmsp);
  4. ?>

...gives me 1969...

Any help here? Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Flanders
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Reno, Nevada US

Post 3+ Months Ago

I'm not sure I understand the question. You want to check to see if a string is a timestamp? Or, you want to be able to use either/or date/timestamp in your function?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

the latter "you [I] want to be able to use either/or date/timestamp in your [mine] function".

But it would be great if I also would be able to "check to see if a string is a timestamp".

Thanks.
  • Flanders
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Reno, Nevada US

Post 3+ Months Ago

Well, I would think that in order to use the either/or method, your function would need to have a "check to see if a string is a timestamp" section in it. From your function name (get_age) it looks like you are trying to calculate how many years old something is.

So, the function would have to check for:
Content of the string (A timestamp is all numbers)
preg_match() function
Then we would know if we have a numeric string (timestamp).

Code: [ Select ]
function get_age($str){
if(preg_match('/^[0-9]{1,}$/',$str)){
//The string is a timestamp
$now=time();
$then=$str;
$diff=$now-$then;  //Number of seconds old
$age=$diff/(60*60*24*365);  //Convert to years old (may want rounding)
return $age;
}else{
//Do something else with the date string
//If it's not a timestamp
}
}
  1. function get_age($str){
  2. if(preg_match('/^[0-9]{1,}$/',$str)){
  3. //The string is a timestamp
  4. $now=time();
  5. $then=$str;
  6. $diff=$now-$then;  //Number of seconds old
  7. $age=$diff/(60*60*24*365);  //Convert to years old (may want rounding)
  8. return $age;
  9. }else{
  10. //Do something else with the date string
  11. //If it's not a timestamp
  12. }
  13. }


I hope this makes sense...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Awesome! Thanks :) The following function worked.
PHP Code: [ Select ]
<?php
    function get_age($date)
    {
        if(preg_match('/^[0-9]{1,}$/',$date))
        {
            $age = (time()-$date)/(31536000);
            return substr($age, 0, 6);
        }
        else
        {
            if($date = strtotime($date))
            {
                $age = (time()-$date)/(31536000);
                return substr($age, 0, 6);
            }
            return false;
        }
        return false;
    }
?>
  1. <?php
  2.     function get_age($date)
  3.     {
  4.         if(preg_match('/^[0-9]{1,}$/',$date))
  5.         {
  6.             $age = (time()-$date)/(31536000);
  7.             return substr($age, 0, 6);
  8.         }
  9.         else
  10.         {
  11.             if($date = strtotime($date))
  12.             {
  13.                 $age = (time()-$date)/(31536000);
  14.                 return substr($age, 0, 6);
  15.             }
  16.             return false;
  17.         }
  18.         return false;
  19.     }
  20. ?>

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 74 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.