Scorpion King
Scorpion King

Reputation: 1938

convert datetime to timeticks in php and mysql

i want to convert datetime.now() to ticks, like is the time is 4:00 PM on june 18th 2010, i want it to be 20100618160000, or some complex number like this. I want to use this number and another field in my database as the primary keys combination to retrieve data. I am using php and mysql so it would be very helpful if someone could send me some code for the same. Thanks.

SJ

Upvotes: 0

Views: 4596

Answers (4)

Gokul Muralidharan
Gokul Muralidharan

Reputation: 161

You can use number_format(($time * 10000000) + 621355968000000000 , 0, '.', ''); where $time represents the the unix timestamp value.

Upvotes: 0

Pekka
Pekka

Reputation: 449843

I want to use this number and another field in my database as the primary keys combination to retrieve data.

Probably not a good idea. There is the theoretical possibility of a collision when two instances of your script get called at the exact same time. You would have to combine this with another unique value to create a safe primary key.

But to answer your question, microtime(true) will give you the current timestamp with microseconds as one long number. strtotime() will translate about any american-format time into a time stamp.

Upvotes: 0

Ken Richards
Ken Richards

Reputation: 3021

Use strtotime to parse any string representation of a date and time into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)

https://www.php.net/manual/en/function.strtotime.php

Upvotes: 1

websch01ar
websch01ar

Reputation: 2123

SELECT UNIX_TIMESTAMP();

Upvotes: 0

Related Questions