
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is the equivalent of MySQL TIME_TO_SEC() method in PHP to convert datetime to seconds?
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:
- Related Articles
- How to convert JS date time to MySQL datetime?
- How to convert JavaScript datetime to MySQL datetime?
- What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?
- Convert INT to DATETIME in MySQL?
- What is the PHP stripos() equivalent in MySQL?
- How to convert timestamp to datetime in MySQL?
- How can we convert TIME and DATETIME values to numeric form in MySQL?
- MySQL query to convert timediff() to seconds?
- How to convert time seconds to h:m:s format in Python?
- How to convert DateTime to a number in MySQL?
- How to convert MySQL datetime to Unix timestamp?
- MySQL Query to convert from datetime to date?
- How to add time in minutes in datetime string PHP?
- Convert the specified Windows file time to an equivalent local time in C#
- What is the PHP equivalent of MySQL's UNHEX()?
