jlp09550 Posted July 27, 2007 Share Posted July 27, 2007 Hello. I've been wondering how to solve this.. and I've seen it in quite a lot of places.. but.. How do I make the date minutes thing not show the first zero? For example, I want: "2 minutes ago.", instead of "02 minutes ago." Thanks! Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 27, 2007 Share Posted July 27, 2007 try if ( strpos ('0',$string)==1) substr_replace($string," ",0,1); Quote Link to comment Share on other sites More sharing options...
matto Posted July 27, 2007 Share Posted July 27, 2007 also something like this should work.... <?php function shorten_minute($min) { return (substr($min,0,1) == 0) ? substr($min,1,1) : $min; } $minutes = "12"; echo shorten_minute($minutes) . "\n"; $minutes = "03"; echo shorten_minute($minutes) . "\n"; ?> Quote Link to comment Share on other sites More sharing options...
jlp09550 Posted July 27, 2007 Author Share Posted July 27, 2007 Hello again. I can't get them to work. For some reason, it blanks them out. I have tried both. The second one worked better than the other one, but still failed. The code: $lastseen_1 = time() - $get_data['lastvisit']; $minute = date('i',$lastseen_1); $second = date('s',$lastseen_1); function shorten_minute($min) { return (substr($min,0,1) == 0) ? substr($min,1,1) : $min; } $minutes = shorten_minute($minute); $seconds = shorten_minute($second); $comb = $minutes + $seconds; echo $comb; ... which outputs "0". Thanks, Jared Quote Link to comment Share on other sites More sharing options...
matto Posted July 27, 2007 Share Posted July 27, 2007 can you post your code so I can see how you are trying to use it... Quote Link to comment Share on other sites More sharing options...
jlp09550 Posted July 27, 2007 Author Share Posted July 27, 2007 can you post your code so I can see how you are trying to use it... It's posted right above you. Quote Link to comment Share on other sites More sharing options...
matto Posted July 27, 2007 Share Posted July 27, 2007 Where does this come from? $get_data['lastvisit']; Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2007 Share Posted July 27, 2007 This is your problem: $comb = $minutes + $seconds; You are adding the minutes and seconds together as numbers - not concatenating the strings. Use this: $comb = $minutes .":". $seconds; Quote Link to comment Share on other sites More sharing options...
jlp09550 Posted July 27, 2007 Author Share Posted July 27, 2007 Where does this come from? $get_data['lastvisit']; It's a time() This is your problem: $comb = $minutes + $seconds; You are adding the minutes and seconds together as numbers - not concatenating the strings. Use this: $comb = $minutes .":". $seconds; That is not what I want. I want to remove the zero in the minute/seconds, if there is one. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2007 Share Posted July 27, 2007 Where does this come from? $get_data['lastvisit']; It's a time() This is your problem: $comb = $minutes + $seconds; You are adding the minutes and seconds together as numbers - not concatenating the strings. Use this: $comb = $minutes .":". $seconds; That is not what I want. I want to remove the zero in the minute/seconds, if there is one. Yes, I understand that. The code you posted works perfectly fine except your are adding the values instead of concatenating them, which is screwing it all up. This: <?php function shorten_minute($min) { return (substr($min,0,1) == 0) ? substr($min,1,1) : $min; } $lastseen_1 = time() - 2100; $minutes = date('i',$lastseen_1); $seconds = date('s',$lastseen_1); echo "Minutes before processing: $minutes<br>"; echo "Seconds before processing: $seconds<br>"; $minutes = shorten_minute($minutes); $seconds = shorten_minute($seconds); echo "Minutes after processing: $minutes<br>"; echo "Seconds after processing: $seconds<br>"; ?> Produced this: Minutes before processing: 09 Seconds before processing: 01 Minutes after processing: 9 Seconds after processing: 1 Isn't that what you wanted? However, this works just as well without all the fuss: $minutes = date('i',$lastseen_1) * 1; $seconds = date('s',$lastseen_1) * 1; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.