JREAM
JREAM

Reputation: 5931

Simple strtotime math

I'm trying to get the total minutes and seconds that have elapsed from the following step by step:

1: strtotime('now') - strtotime('-1 day');

2: Ill get something back like 120 lets say...

3: So... 120 = 2 minutes -- My problem is the remainder though!

4: Examples:

    130 = 2 minutes 10 seconds
    130 / 60 = 2.1666~

    121 = 2 minutes 1 seconds
    121 / 60 = 2.0166~

    122 = 2 minutes 2 seconds
    122 / 60 = 2.0366~

PS: I never had a gift for Math.

Upvotes: 0

Views: 643

Answers (2)

Teneff
Teneff

Reputation: 32158

This is how you can use PHP's DateTime::diff which returns DateInterval

   $dt = new DateTime();
   $dt2 = new DateTime("-120 seconds");
   $diff = $dt->diff( $dt2 );
   echo( $diff->format("%i min and %s sec") );

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173562

You can also use DateTime math (php >= 5.3):

$d1 = new DateTime(strtotime('now'));
$d2 = new DateTime(strtotime('-1 days'));

$diff = $d1->diff($d2);

echo "{$diff->i} minutes, {$diff->s} seconds\n";

Upvotes: 1

Related Questions