Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert datetime to unix timestamp

Aggelos
Guest
 
Posts: n/a
#1: Oct 23 '06
I suppose that has been discussed. I search on google but couldn't find
a short answer.
I have a datetime field: 2006-10-10 12:00:00
And I want to get that in seconds.
Should I break the date and time and calculate it in two steps ?
or is there a quick way to do it in one step ?
I am trying to set the rate of a process execution between two
datetimes and I am going to need everydate in seconds.

Rik
Guest
 
Posts: n/a
#2: Oct 23 '06

re: Convert datetime to unix timestamp


Aggelos wrote:
Quote:
I suppose that has been discussed. I search on google but couldn't
find a short answer.
I have a datetime field: 2006-10-10 12:00:00
And I want to get that in seconds.
Should I break the date and time and calculate it in two steps ?
or is there a quick way to do it in one step ?
I am trying to set the rate of a process execution between two
datetimes and I am going to need everydate in seconds.
When you say 'field', do you mean a database field? Most databases offer
their own logic for this, for instance in MySQL:

SELECT UNIX_TIMESTAMP(NOW())

or
SELECT UNIX_TIMESTAMP(`field`) FROM `table`

Other databases often have similar conststructs.
In PHP, you could use strtotime(), but if the data comes from a database I
strongly recommend to let the database handle it.
--
Grtz,

Rik Wasmus


Aggelos
Guest
 
Posts: n/a
#3: Oct 23 '06

re: Convert datetime to unix timestamp


In PHP, you could use strtotime(), but if the data comes from a database I
Quote:
strongly recommend to let the database handle it.
--
Grtz,
>
Rik Wasmus
The datetime in the database is stored from PHP using date(Y-m-d H:i:s)
so when I pull it from MySQL database
I am expecting to convert it from Datetime format to Unixtimestamp.
I found a function in PHP.net str2time it is in the second post from
the top
http://uk.php.net/manual/en/function.mktime.php#67996

Thats how I do it now.
But I don't understand what can be different in PHP from MySQL built in
function.
All they have to do is add seconds since 1/1/1970 plus the current time
seconds.

Thanks for your post anyway. :)

Joshua Beall
Guest
 
Posts: n/a
#4: Oct 23 '06

re: Convert datetime to unix timestamp


Aggelos wrote:
Quote:
Quote:
In PHP, you could use strtotime(), but if the data comes from a database I
strongly recommend to let the database handle it.
<snip>
Quote:
But I don't understand what can be different in PHP from MySQL built in function.
The point is that if you can get the database to give you your data in
the format you want it, that's preferable to having to massage the date
that MySQL gives you into the format you want. So, if the database
allows you to transform your data the way you want, why not take
advantage of it?

Functionally, you end up with the same result, so I understand your
confusion on the reasoning.

-Josh

Closed Thread