dzm
dzm

Reputation: 23574

PHP convert datetime to seconds

I have a datetime value in mysql '2010-12-08 16:12:12'
that I'd like to get the seconds to that date using PHP,
so basically a PHP equivalent of mysql :

TIME_TO_SEC(TIMEDIFF('2010-12-08 16:12:12',now()))

Upvotes: 19

Views: 60695

Answers (4)

Anush Prem
Anush Prem

Reputation: 1471

try

$time_diff = time() - strtotime('2010-12-08 16:12:12');

Upvotes: 1

phirschybar
phirschybar

Reputation: 8599

Use mktime()

http://php.net/manual/en/function.mktime.php

Upvotes: -2

davidtbernal
davidtbernal

Reputation: 13694

<?php

$date1 = new DateTime("2010-12-08 16:12:12");
$now = new DateTime();

$difference_in_seconds = $date1->format('U') - $now->format('U');

->format('U') turns it into a unix timestamp.

Upvotes: 30

ajreal
ajreal

Reputation: 47331

huh ? these function are from mysql ...

For PHP, you replace it using strtotime

$diff = strtotime('2010-12-08 16:12:12')-time();

details : http://php.net/manual/en/function.strtotime.php

Upvotes: 21

Related Questions