What is the equivalent of MySQL TIME_TO_SEC() method in PHP to convert datetime to seconds?

MySQLMySQLi Database

The function TIME_TO_SEC() can be used in MySQL. 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().

First, you need to install XAMPP server to run your PHP program.

After installing XAMPP successfully in C drive, here is the location wherein you need to include the PHP file. The snapshot is as follows:

Note: Here, I have changed port of Apache to 8086 because default port was held by another program. This is done to begin running PHP program.

Therefore, if in your system the default port is working then use it otherwise you can use your updated port in address bar. The url is as follows:

http://localhost:yourPortNumber/locationofphpfile/

So, here the port number is 8086, ’locationofphpfile’ is ‘phpproject’. Here is the snapshot of port number:

First, open the editor and write the below code to convert datetime to seconds. The PHP code is as follows.

Save the program with .php extension like ‘yourFileName.php’. I have saved with ‘DifferenceInSeconds.php’.

DifferenceInSeconds.php

<<?php
$MySQLDateTime='2019-01-29 13:50:40';
echo "<h1>The strtotime value is:"."</h1>".strtotime($MySQLDateTime)."<br>";
echo "<h1>The time value is:"."</h1>".time()."<br>";
$diffenceInSeconds = strtotime($MySQLDateTime)-time();
echo "<h1>The difference of both time in seconds is:"."</h1>".$diffenceInSeconds;
?>

The snapshot of PHP code is as follows:

Now open any browser and paste URL in address bar.

http://localhost:yourPortNumber/locationofphpfile/

The port number is 8086 in my system. Check your port number in XAMPP server. Paste the following url in address bar. Here, the phpproject is our folder in XAMPP/ htdocs/ wherein we have saved our PHP file:

http://localhost:8086/phpproject/

After pressing enter key you will get a page where your program is saved. The snapshot is as follows:

Now click the above php file ‘DifferenceInSeconds.php’. You will get the following output:

raja
Published on 26-Feb-2019 10:03:18
Advertisements