Skip to content

Instantly share code, notes, and snippets.

@moklett
Created November 5, 2012 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moklett/4018049 to your computer and use it in GitHub Desktop.
Save moklett/4018049 to your computer and use it in GitHub Desktop.
Date/Time Formats from Unix Timestamp

Unix Timestamp (Seconds since epoch)

Generating from Ruby

t = Time.now
# => 2012-11-05 11:12:25 -0500
t.to_i
# => 1352131945
tu = t.utc
# => 2012-11-05 16:12:25 UTC
tu.to_i
# => 1352131945

Parsing with PHP

php > date_default_timezone_set("UTC");
php > $t = new DateTime("@1352131945");
php > echo date_format($t, "Y-m-d H:i:s");
2012-11-05 16:12:25
php > echo date_format($t, "Y-m-d H:i:s e");
2012-11-05 16:12:25 +00:00
php > $t->setTimezone(new DateTimeZone("America/New_York"));
php > echo date_format($t, "Y-m-d H:i:s e");
2012-11-05 11:12:25 America/New_York

RFC 2822

Generating from Ruby (w/ ActiveSupport)

>> t = Time.now
=> Mon Nov 05 11:39:09 -0500 2012
>> tu = t.utc
=> Mon Nov 05 16:39:09 UTC 2012
>> t.to_s(:rfc822)
=> "Mon, 05 Nov 2012 16:39:09 +0000"
>> tu.to_s(:rfc822)
=> "Mon, 05 Nov 2012 16:39:09 +0000"

Parsing with Ruby

irb(main):001:0> require 'time'
=> true
irb(main):002:0> t = Time.parse("Mon, 05 Nov 2012 16:39:09 +0000")
=> 2012-11-05 11:39:09 -0500

Parsing with PHP

php > date_default_timezone_set("UTC");
php > $t = new DateTime("Mon, 05 Nov 2012 16:39:09 +0000");
php > echo date_format($t, "Y-m-d H:i:s");
2012-11-05 16:39:09
php > echo date_format($t, "Y-m-d H:i:s e");
2012-11-05 16:39:09 +00:00
php > $t->setTimezone(new DateTimeZone("America/New_York"));
php > echo date_format($t, "Y-m-d H:i:s e");
2012-11-05 11:39:09 America/New_York
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment