简体   繁体   中英

php strtotime in seconds and minutes

i use ths method to find the difference between two timestamp and get the number of seconds between those two times, and i refresh the information with jquery like a counter.

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;

When the difference is more than 60 seconds, the $time comes back to 0, like a reset.

i would like to display 1 min XX s for example

The s flag for date() will never return a value greater than 59 as it only represents the current number of seconds of a given time which can never be more than 59 before rolling over into a new minute.

If you want the total number of seconds you can actually remove your second line of code as the difference between two Unix Timestamps is always in seconds:

$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;

If you want to display this as minutes and seconds you can use DateTime() which offers better tools for this:

$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');

format the date

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;

Pass time like 1 & now 2

function diffrencePassTimeAction($DataTime){
    $im = $DataTime - strtotime("now");
  return $im;
}

Future time like 2 & now 1

function diffrenceFuturTimeAction($DataTime){
    $im = strtotime("now") - $DataTime;
  return $im;
}

this function delete (-less)

function diffrencePassTimeAction($DataTime){
    if ($DataTime > 0)
        return $DataTime - strtotime("now");
    else
        return strtotime("now"); // OR return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM