php - Calculate difference between time in DB and Current Time -


first off - using mysqli procedural method of using mysql via php

i trying create way click button set "timer". understand, best way (on dynamic timer - ie: 5 minutes click) calculate time() 5 minutes "now".

i want save db in case user disconnects page , reconnects (username/password login). way, when log in, keep remaining time.

now code:

i using pull dt variable sql (yyyy-mm-dd hh-mm-ss):

$expirationtime = new datetime();  if ($result->num_rows > 0) {     while($row = $result->fetch_assoc()) {         $expirationtime = $row['worktimer'];     } }  $time = strtotime($expirationtime); $expirationtime = date("y-m-d h:i:s", $time); 

then pull current time:

$currenttime = date('y-m-d h:i:s'); 

i have done echos on both of these , displaying correctly. want figure out difference. have tried multiple ways of doing this, nothing seems work. did:

$countdown = $currenttime->diff($expirationtime); $countdown = $countdown->format("y-m-d h:i:s", $countdown); 

with error of fatal error: call member function diff() on non-object in...

what might problem?

thanks!


i have updated of codes per comment's suggestions to:

$currenttimet = date('y-m-d h:i:s'); $currenttime = strtotime($currenttimet);  $time = date("y-m-d h:i:s", $expirationtime); $expirationtime = strtotime($expirationtime);  $countdown = $currenttime->diff($expirationtime); $countdown = $countdown->format("y-m-d h:i:s", $countdown); echo "c ".$currenttime."<br/> e ".$expirationtime."<br/>d ".$countdown; 

same error.

it least elegant solution cut timestrings both "d,m,y,h,i,s"-substrings , feed them http://php.net/manual/de/function.mktime.php. calc difference of timestamps.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -