PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa

2013-02-15T04:39:16

im using php 5.4.6 and MySQL 5.5.29 and I get trouble by converting a UNIX TIMESTAMP to MYSQL DATETIME and vice versa. Mysql and php wun on the same local machine.

I have a simple mysql table

CREATE TABLE Entry(
id SERIAL PRIMARY KEY,
created DATETIME NOT NULL);

To insert data I use php PDO and mysql NOW(). This works fine, the correct datetime is stored in the database.

My plan is to work with unix timestamps on client side (php & mysql on server side).

So I would like to deliver unix timestamps to my client. Therefore I use MySql UNIX_TIMESTAMP() function to convert it directly in the query.

So a sample query looks like this:

SELECT created, UNIX_TIMESTAMP(created) AS TS FROM Entry

The result: created = 2013-02-14 20:47:35 TS = 1360871255

So now I want to do the other way, I pass the a UNIX Timestamp, and want to compare it with Entries in my Database. Unfortunetly Im not able to write a PHP script that works. I don't know why, but when I m passing the same timestamp (1360871255) to PHP I do not get 2013-02-14 20:47:35 with this method:

public static function toDateTime($unixTimestamp){
    return date("Y-m-d H:m:s", $unixTimestamp);
}

When I call toDateTime(1360871255) will return 2013-02-14 20:02:35 which is not the original DateTime.

I know, I dont need to format 1360871255 to a Y-m-d H:m:s to use it in MYSQL, but 1360871255 seems not to be the time that I expected (and MYSQL UNIX_TIMESTAMP has returned).

What I want to do is a simple query that shows me Entries that are older than a certain timestamp, something simple like this:

SELECT * FROM Entry WHERE created < 1360871255

but as I mentioned before, the query result is not the expected, because 1360871255 seems not to be the correct time.

I do not specify any special timezone for the mysql connection in php.

Any suggestions?

Copyright License:
Author:「sockeqwe」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/14883617/php-mysql-converting-unix-timestamp-to-datetime-and-vice-versa

About “PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa” questions

im using php 5.4.6 and MySQL 5.5.29 and I get trouble by converting a UNIX TIMESTAMP to MYSQL DATETIME and vice versa. Mysql and php wun on the same local machine. I have a simple mysql table CRE...
A unix timestamp is an int which gives the number of seconds since January 1, 1970, UTC. Is there a way to convert the .NET timestamp to unix timestamp using python ? or can this only be done in C...
I have written below microservice to convert unix timestamp to a format like Dec 01,2017 and vice versa and deployed here timestamp Is their any better way to do this with out third party NPM var...
I have a supposed Unix date format coming through in a json feed from a third party that looks like this: /Date(1391741913713+1100)/ Is there any way I can parse that to a DateTime object? I'm ...
I found this sample source code for C# from How to convert a Unix timestamp to DateTime and vice versa?: DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0); DateTime epoch = new DateTime(1970,...
Is this possible? I found a few solution for converting to and from localDateTime like can be seen here But I can't find a solution for Joda Instant...
How can I Convert Java Timestamp (Timestamp data type) to MySQL timestamp vice versa?
I'm trying to convert a SAS datetime to a UNIX timestamp but am getting an 8 hour difference in my results. I think it's somehow related to a timezone issue but I can't figure out how. To start w...
Lets say today is the 08.20.2014. I want to get the date from "today" additional 30 years (08.20.2044) with PHP and insert it into my mysql Database with it´s Datetime field. How do I correctly
So here's the thing, I have the database 'example' with two tables: 'exampletable1' contains two columns 'ID','datetime' 'exampletable2' contains two columns 'id2','ti

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.