Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime to Integer

Tags:

datetime

mysql

I have one problem... There is a table in my MySQL database that stores to-do list entries inside a JQuery-type calendar.
I had to generate a calendar_id that will generate a reminder once I created timestamp (considered as time I clicked on any of the calender dateboxes, to key in some to-do tasks - put it simple: created datetime).

This to-do list activities app is an external application that I've been working on to integrate with my own management system. I noticed that,the timestamp column is in int(11) format, so whatever timestamp entered will be converted into integer.

For example, take a look at this:

2012-02-22 15:31:24

converted to

1329899400

How can we convert datetime to this format? It's not in seconds when I tried:

intval(floor($datetime/86400));

Any help?

like image 402
foxns7 Avatar asked Feb 22 '12 16:02

foxns7


People also ask

How do I convert datetime to numbers?

In this method, we are using strftime() function of datetime class which converts it into the string which can be converted to an integer using the int() function. Returns : It returns the string representation of the date or time object. Code: Python3.

How do you convert time to integer in Python?

Example 1: Integer timestamp of the current date and timeConvert the DateTime object into timestamp using DateTime. timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

How do I convert a date to an integer in R?

To convert Date to Numeric format in R, use the as. POSIXct() function and then you can coerce it to a numeric value using as. numeric() function.


2 Answers

FROM UNIXTIME can format UNIX timestamps into datetime fields:

SELECT FROM_UNIXTIME(time)
FROM ...

The reverse function would be UNIX_TIMESTAMP.


Alternatively you can do it in PHP, if available:
To store a date into the DB format it like this:

$datetimeStr = '2012-02-22 15:31:24';
$datetime = strtotime($datetimeStr);

To retrieve it from the DB and format it to the original format, use something like this:

$dateTimeFromDB = '1329921084';
$datetimeStr = date('Y-m-d H:i:s', $dateTimeFromDB);
like image 174
Chris Avatar answered Oct 16 '22 09:10

Chris


Here's a nice MySQL's UNIX_TIMESTAMP() function for you

like image 33
Mchl Avatar answered Oct 16 '22 08:10

Mchl