0

I need to compare bentween a time taken from a database to the current time.

$DBtime = "2013-10-29 17:38:55";

this is the format of the arrays in the database.

How can I compare it with the current time?

Im not sure how, but maybe converting DBtime to Unixtime then:

(CurrentUnixTime - dbUnixTime) = x

Or maybe, we can take the 17:38 and compare it somehow with date("G:i");

Thank you! I hope you understand what I mean.

4 Answers 4

1

You can transform it into a UNIX timestamp using strtotime and then subtract the current timestamp by it.

$DBtime = "2013-10-29 17:38:55";
$db_timestamp = strtotime($DBtime);
$now = time();
$difference = $now - $db_timestamp;
echo $difference;

This will give you the difference in seconds.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for anwsering! I appreciate that. That was exactly what I looked for... I knew what I had to do but didn't knew the function ;) Thanks, I'll use that.
1

You can convert the DBtime string to a unix timestamp in PHP using strtotime. In MySQL, you can use UNIX_TIMESTAMP when querying the column.

time() - strtotime($DBtime)

1 Comment

Wow, what a quick reply!, I appreciate that. And that function, is amazing! could be so usefull! I'll surly use that.
0
$date1 = new DateTime('2013-10-29 17:38:55');
$date2 = new DateTime('2013-11-29 18:28:21');
$diff = $date1->diff($date2);
echo $diff->format('%m month, %d days, %h hours, %i minutes');

3 Comments

Looks... intresting. Could you explain me the last two lines? Thanks.
@user2054911, diff is function of DateTime class which $date1 is object of. It returns an object of DateInterval class. Function format uses common date literals to output what you want in needed format. -> is used to call functions on objects.
Sounds intresting and usefull, never heard about it and im going to study abit about it, tx.
0
$DBtime = "2013-10-29 17:38:55";

// Set whatever timezone was used to save the data originally
date_default_timezone_set('CST6CDT');

// Get the current date/time and format the same as your input date
$curdate=date("Y-m-d H:i:s", time());

if($DBtime == $curdate) {
    // They match, do something
} else {
    // They don't match
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.