# Time in Rails

**URL:** <https://www.ruby-forum.com/t/time-in-rails/76787>\
**Category:** Rails\
**Created:** [November 21, 2006, 4:48am UTC](https://www.ruby-forum.com/t/time-in-rails/76787 "2006-11-21T04:48:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 4:48am UTC](https://www.ruby-forum.com/t/time-in-rails/76787/1 "2006-11-21T04:48:57Z")

</div>

Hello,

I have a background in PHP. I have been working with rails for a little  
wihle now and somethign i still am not understanding is how ruby handles  
time stamps. What method can i use to get the current time stamp? Also  
how can i convert that timestamp in to an “english” time?

I need to work out times based on weeks. So for example i would have  
done something like this in php

$time\_now = time();  
$time\_one\_week\_form\_now = 60 \* 60 \* 24 \* 7 + time();

how would i do this in ruby?

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 5:59am UTC](https://www.ruby-forum.com/t/time-in-rails/76787/2 "2006-11-21T05:59:59Z")

</div>

Stewart wrote:

> Hello,
> 
> I have a background in PHP. I have been working with rails for a little  
> wihle now and somethign i still am not understanding is how ruby handles  
> time stamps. What method can i use to get the current time stamp? Also  
> how can i convert that timestamp in to an “english” time?
> 
> I need to work out times based on weeks. So for example i would have  
> done something like this in php
> 
> $time\_now = time();  
> $time\_one\_week\_form\_now = 60 \* 60 \* 24 \* 7 + time();
> 
> how would i do this in ruby?

ANSWER:

[http://wiki.rubyonrails.com/rails/pages/PhpDateAndTimeFunctions](http://wiki.rubyonrails.com/rails/pages/PhpDateAndTimeFunctions)

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 6:07am UTC](https://www.ruby-forum.com/t/time-in-rails/76787/3 "2006-11-21T06:07:29Z")

</div>

On 11/20/06, Stewart [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $time\_now = time();  
> $time\_one\_week\_form\_now = 60 \* 60 \* 24 \* 7 + time();
> 
> how would i do this in ruby?

Not much different…

time\_now = Time.now  
time\_one\_week\_from\_now = time\_now + 60 \* 60 \* 24 \* 7

[http://www.rubycentral.com/book/ref\_c\_time.html](http://www.rubycentral.com/book/ref_c_time.html)  
[http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html](http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/DateTime.html)

There’s some other date helpers in rails that may be useful too  
[http://rubyonrails.com/rails/classes/ActionView/Helpers/DateHelper.html](http://rubyonrails.com/rails/classes/ActionView/Helpers/DateHelper.html)

–

Chris M.  
Web D.  
Open Source & Web Standards Advocate

> **[Chriscodes.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=chriscodes.com)**
>
> Painless, quick delivery of your domain name. Fast and professional service.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/time-in-rails/76787/4 "2006-11-21T18:08:03Z")

</div>

Alex W. wrote:

> > 
> 
> puts then.strftime(‘%Y-%m-%d’)  
> #=\> 2006-11-27
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

require ‘rubygems’  
require ‘ruby-units’  
require ‘chronic’

now = ‘now’.time  
next\_week = now + ‘1 week’.unit

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/time-in-rails/76787/5 "2006-11-21T22:45:13Z")

</div>

On Tue, Nov 21, 2006 at 06:21:13AM +0100, Alex W. wrote:

> puts then.strftime(‘%Y-%m-%d’)  
> #=\> 2006-11-27

In addition you can set the default way Rails prints timestamps and  
dates as discussed in the thread:

[http://www.ruby-forum.com/topic/57923](http://www.ruby-forum.com/topic/57923)

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 21, 2006, 6:21am UTC](https://www.ruby-forum.com/t/time-in-rails/76787/6 "2006-11-21T06:21:13Z")

</div>

Stewart wrote:

> Hello,
> 
> I have a background in PHP. I have been working with rails for a little  
> wihle now and somethign i still am not understanding is how ruby handles  
> time stamps. What method can i use to get the current time stamp? Also  
> how can i convert that timestamp in to an “english” time?
> 
> I need to work out times based on weeks. So for example i would have  
> done something like this in php
> 
> $time\_now = time();  
> $time\_one\_week\_form\_now = 60 \* 60 \* 24 \* 7 + time();
> 
> how would i do this in ruby?

easy as pie

now = Time.now  
then = 1.week.from\_now

puts then.strftime(’%Y-%m-%d’)  
#=\> 2006-11-27
