Skip to content

Instantly share code, notes, and snippets.

@ryanmerritt
Forked from cam-gists/time.php
Created September 18, 2012 10:15
Show Gist options
  • Save ryanmerritt/3742425 to your computer and use it in GitHub Desktop.
Save ryanmerritt/3742425 to your computer and use it in GitHub Desktop.
PHP: Unix Time Methods
<?php
print time();
//1324402770
$unixTime = time();
print_r(getdate($unixTime));
// Array
// (
// [seconds] => 48
// [minutes] => 54
// [hours] => 12
// [mday] => 20
// [wday] => 2
// [mon] => 12
// [year] => 2011
// [yday] => 353
// [weekday] => Tuesday
// [month] => December
// [0] => 1324403688
// )
//Formatting a Unix Time
print date("r", $unixTime);
//Tue, 20 Dec 2011 12:54:48 -0500
print date("m/d/y h:i:s a", $unixTime);
//12/20/11 12:54:48 pm
print date("m/d/y h:i:s a");
//12/20/11 01:12:11 pm
print date("jS \of F Y", $unixTime);
//20th of December 2011
print date("r", mktime(12, 0, 0, 1, 20, 1987));
//Tue, 20 Jan 1987 12:00:00 -0500
print date("r", mktime(0, 0, 0, date("n"), date("j"), date("Y")));
//Tue, 20 Dec 2011 00:00:00 -0500
print date("r", mktime(23, 59, 59, date("n"), date("j"), date("Y")));
//Tue, 20 Dec 2011 23:59:59 -0500
print strtotime("now");
//1324407707
//Parsing an English Date to Unix Time
print date("r", strtotime("now"));
//Tue, 20 Dec 2011 14:01:51 -0500
print strtotime("+1 week");
//1325012569
print date("r", strtotime("+1 week"));
//Tue, 27 Dec 2011 14:03:03 -0500
print date("r", strtotime("next month"));
//Fri, 20 Jan 2012 14:04:20 -0500
print date("r", strtotime("next month", mktime(0, 0, 0)));
//Fri, 20 Jan 2012 00:00:00 -0500
print date("r", strtotime("next month", mktime(0, 0, 0, 1, 31)));
//Thu, 03 Mar 2011 00:00:00 -0500
//PHP’s DateTime and DateTimeZone Objects
$dt = new DateTime("now");
print $dt->format("r");
//Tue, 20 Dec 2011 16:28:32 -0500
$dt = new DateTime("December 31 1999 12:12:12 EST");
print $dt->format("r");
//Fri, 31 Dec 1999 12:12:12 -0500
print $dt->format(DATE_ATOM);
//2011-12-20T15:57:45-05:00
print $dt->format(DATE_ISO8601);
//2011-12-20T15:57:45-0500
print $dt->format(DATE_RFC822);
//Tue, 20 Dec 11 15:57:45 -0500
print $dt->format(DATE_RSS);
//Tue, 20 Dec 2011 15:57:45 -0500
print date_default_timezone_get();
//America/New_York
$dt = new DateTime();
print $dt->getTimeZone()->getName();
//UTC
$dt = new DateTime();
print $dt->format("r");
//Tue, 20 Dec 2011 20:57:45 +0000
$tz = new DateTimeZone("America/New_York");
$dt->setTimezone($tz);
print $dt->gt;format("r");
//Tue, 20 Dec 2011 15:57:45 -0500
$tz = new DateTimeZone("America/New_York");
$july = new DateTime("7/1/2011");
$july->setTimezone($tz);
print $july->>format("r");
//Thu, 30 Jun 2011 20:00:00 -0400
$result = $db->query("SELECT int_date FROM some_table");
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$dt = new DateTime();
$tz = new DateTimeZone($_SESSION["userTZ"]);
$dt->setTimestamp($row["int_date"]);
$dt->setTimezone($tz);
print $dt->format("r");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment