# Ruby datetime to mysql

**URL:** <https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148>\
**Category:** Ruby\
**Created:** [February 28, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148 "2007-02-28T15:14:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/1 "2007-02-28T15:14:52Z")

</div>

I’m missing the boat on datetime. I’m trying to convert Ruby datetime to  
be compatible with mysql. I’m not getting any errors but the results  
always end up 0000-00-00-0000

I think it may have to do with the mysql requirement for 2 digit month  
and hours or the seconds. I’m not sure. When I output my conversion to  
my browser the output looks correct.

Can someone shed some light on how to do this.

Thanks

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/2 "2007-02-28T15:32:21Z")

</div>

> I’m missing the boat on datetime. I’m trying to convert Ruby datetime to  
> be compatible with mysql. I’m not getting any errors but the results  
> always end up 0000-00-00-0000

I don’t believe you should need to do any conversion, Ruby’s date types  
should work as parameters to prepared statements and date fields  
returned by selects shouldn’t need conversions, either. How are you  
connecting to mysql, and what problems are you having? It would help  
if you posted an example.

-Tim

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/3 "2007-02-28T17:34:14Z")

</div>

Hi Tim  
I don’t have access to my code at the moment but here is the situation.  
I have a simple counter on a web page that updates a customer record 0n  
every visit. I can use the mysql built in timestamp however I have  
another script that runs daily at midnight which of course updates the  
timestamp on all records. So I need to add a datetime string to my  
update with a date that mysql understands.

I’m new to Ruby scripting so time and datetime are a bit daunting.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/4 "2007-02-28T18:57:39Z")

</div>

Hi Again

The one script is eruby index.rhtml the other is not ruby and it does  
not use a date or time it simply resets the users quota which also  
unfortunately updates the time stamp.

I need to make a string that {#date} is the format that mysql uses,

This is from memory. I connect via dbi to a remote db server on the lan.

dbh.do(“UPDATE member SET counter = counter + 1, ip =  
‘#{ENV[‘REMOTE\_ADDR’]}’, datetime =’{#date}’ WHERE username =  
‘#{ENV[‘REMOTE\_USER’]}’”)  
end

Peter

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/5 "2007-02-28T18:01:22Z")

</div>

> I can use the mysql built in timestamp however I have  
> another script that runs daily at midnight which of course updates the  
> timestamp on all records. So I need to add a datetime string to my  
> update with a date that mysql understands.

Assuming their both ruby scripts, I’m not sure I understand why one  
would understand ruby time objects, but not the other.

I think to diagnose this further, we really just need to look at how  
you’re implementing this, what error messages you’re getting, how  
you’re accessing mysql…

Cheers,  
-tim

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/6 "2007-02-28T19:05:39Z")

</div>

On Feb 28, 10:55 am, peter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to make a string that {#date} is the format that mysql uses,

See the (confusingly-tersely-named) Time#strftime method. It will let  
you create whatever format you want. You can even wrap it in your own  
method, like:

class Time  
def as\_mysql  
strftime “…”  
end  
end

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 7:19pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/7 "2007-02-28T19:19:02Z")

</div>

Gavin K. wrote:

> See the (confusingly-tersely-named) Time#strftime method. It will let

strftime is named after the function in the Standard C library. It  
creates a \_str\_ing by \_f\_ormatting a \_t\_ime value.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [February 28, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/ruby-datetime-to-mysql/87148/8 "2007-02-28T20:00:47Z")

</div>

Hi –

On Thu, 1 Mar 2007, Tim H. wrote:

> Gavin K. wrote:
> 
> > See the (confusingly-tersely-named) Time#strftime method. It will let
> 
> strftime is named after the function in the Standard C library. It  
> creates a \_str\_ing by \_f\_ormatting a \_t\_ime value.

Shades of “When you assume…” 🙂

David
