Jump to content

[SOLVED] Removing Zero at Beginning of DATE Minutes


jlp09550

Recommended Posts

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!

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.