# Best way to get epoch seconds from mysql datetime value

**URL:** <https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625>\
**Category:** Ruby\
**Created:** [November 11, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625 "2006-11-11T17:33:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/1 "2006-11-11T17:33:59Z")

</div>

Hey there all,

if i have a mysql datetime value that looks like this:  
some\_time = ‘2006-05-11 05:14:14’

and i want to get seconds since the unix epoch, how would i go about  
that ?

thanks

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/2 "2006-11-11T17:42:01Z")

</div>

On 11/nov/06, at 17:32, shawn bright wrote:

> Hey there all,
> 
> if i have a mysql datetime value that looks like this:  
> some\_time = ‘2006-05-11 05:14:14’
> 
> and i want to get seconds since the unix epoch, how would i go  
> about that ?
> 
> thanks

$ irb

> > require ‘date’  
> > =\> true
> 
> > some\_time = ‘2006-05-11 05:14:14’  
> > =\> “2006-05-11 05:14:14”
> 
> > time = Time.parse(some\_time)  
> > =\> Thu May 11 05:14:14 0200 2006
> 
> > time.to\_i  
> > =\> 1147317254

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/3 "2006-11-11T17:44:02Z")

</div>

On Sun, 12 Nov 2006, shawn bright wrote:

> Hey there all,
> 
> if i have a mysql datetime value that looks like this:  
> some\_time = ‘2006-05-11 05:14:14’
> 
> and i want to get seconds since the unix epoch, how would i go about that ?
> 
> thanks

require ‘time’

t = Time.parse some\_time  
epoch\_seconds = t.to\_i

-a

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/4 "2006-11-11T18:02:05Z")

</div>

Hey great ! thanks, this works for me.  
sk

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/5 "2006-11-11T19:14:13Z")

</div>

i suppose i could use that, i am working with mysql 5 ( oh, the luxury )  
i am just not that knowledgeable with it.  
thanks

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/6 "2006-11-11T18:54:10Z")

</div>

Hi,

On Son 12.11.2006 01:32, shawn bright wrote:

> Hey there all,
> 
> if i have a mysql datetime value that looks like this:  
> some\_time = ‘2006-05-11 05:14:14’
> 
> and i want to get seconds since the unix epoch, how would i go about  
> that ?

If you have the possibility to use mysql why not to use the builtin  
function:

UNIX\_TIMESTAMP(date)

from [http://www.mysql.org/doc/refman/4.1/en/date-and-time-functions.html](http://www.mysql.org/doc/refman/4.1/en/date-and-time-functions.html)

Due the fact that I don’t know which version of mysql you use I have  
looked into the lowest possible version 😉

Hth

Aleks

---

<div class="post-metadata">

**Author:** ![shawn\_bright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@shawn\_bright](https://www.ruby-forum.com/u/shawn_bright)\
**Post date:** [November 11, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/best-way-to-get-epoch-seconds-from-mysql-datetime-value/75625/7 "2006-11-11T19:22:13Z")

</div>

On Son 12.11.2006 03:12, shawn bright wrote:

> i suppose i could use that, i am working with mysql 5 ( oh, the luxury)  
> i am just not that knowledgeable with it.

[http://www.mysql.org/doc/refman/5.0/en/date-and-time-functions.html](http://www.mysql.org/doc/refman/5.0/en/date-and-time-functions.html)

Same there =\> UNIX\_TIMESTAMP(date)

Hth

Aleks
