Time in Rails

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?

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

On 11/20/06, Stewart [email protected] 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.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

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

Alex W. wrote:

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


Posted via http://www.ruby-forum.com/.

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

now = ‘now’.time
next_week = now + ‘1 week’.unit

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


Cheers,

  • Jacob A.

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