Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()))
like image 301
dzm Avatar asked Dec 08 '10 20:12

dzm


People also ask

How to convert date-time to seconds in PHP?

If you want to convert datetime to seconds use the strtotime() from PHP. The MySQL syntax is as follows: SELECT TIME_TO_SEC(ABS(timediff('yourDateTimeValue',now()))); Now you can convert PHP datetime to seconds with the help of strtotime().

How to convert DateTime into time in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

Why use strtotime?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.

How convert seconds to hours minutes and seconds in PHP?

php //PHP program to convert seconds into //hours, minutes, and seconds $seconds = 6530; $secs = $seconds % 60; $hrs = $seconds / 60; $mins = $hrs % 60; $hrs = $hrs / 60; print ("HH:MM:SS-> " . (int)$hrs .


1 Answers

<?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.

like image 127
davidtbernal Avatar answered Sep 23 '22 00:09

davidtbernal