A very verbose loop. The construct function for the DateTime class isn't working properly for me but this works.
<?php
$date = "2011/03/20";
$date = explode("/", $date);
$time = "07:16:17";
$time = explode(":", $time);
$tz_string = "America/Los_Angeles"; // Use one from list of TZ names http://php.net/manual/en/timezones.php
$tz_object = new DateTimeZone($tz_string);
$datetime = new DateTime();
$datetime->setTimezone($tz_object);
$datetime->setDate($date[0], $date[1], $date[2]);
$datetime->setTime($time[0], $time[1], $time[2]);
print $datetime->format('Y/m/d H:i:s'); // Prints "2011/03/20 07:16:17"
?>
日付・時刻
- 導入
- インストール/設定
- 定義済み定数
- DateTime — DateTime クラス
- DateTime::add — 年月日時分秒の値を DateTime オブジェクトに加える
- DateTime::__construct — 新しい DateTime オブジェクトを返す
- DateTime::createFromFormat — 指定した書式でフォーマットした新しい DateTime オブジェクトを返す
- DateTime::diff — ふたつの DateTime オブジェクトの差を返す
- DateTime::format — 指定した書式でフォーマットした日付を返す
- DateTime::getLastErrors — 警告およびエラーを返す
- DateTime::getOffset — タイムゾーンのオフセットを返す
- DateTime::getTimestamp — Unix タイムスタンプを取得する
- DateTime::getTimezone — 指定した DateTime に関連するタイムゾーンを返す
- DateTime::modify — タイムスタンプを変更する
- DateTime::__set_state — __set_state ハンドラ
- DateTime::setDate — 日付を設定する
- DateTime::setISODate — ISO 日付を設定する
- DateTime::setTime — 時刻を設定する
- DateTime::setTimestamp — Unix タイムスタンプを用いて日付と時刻を設定する
- DateTime::setTimezone — DateTime オブジェクトのタイムゾーンを設定する
- DateTime::sub — 年月日時分秒の値を DateTime オブジェクトから引く
- DateTime::__wakeup — __wakeup ハンドラ
- DateTimeZone — DateTimeZone クラス
- DateTimeZone::__construct — 新しい DateTimeZone オブジェクトを作成する
- DateTimeZone::getLocation — タイムゾーンの位置情報を返す
- DateTimeZone::getName — タイムゾーンの名前を返す
- DateTimeZone::getOffset — GMT からのタイムゾーンのオフセットを返す
- DateTimeZone::getTransitions — タイムゾーンの変遷を返す
- DateTimeZone::listAbbreviations — 夏時間、オフセットおよびタイムゾーン名を含む連想配列を返す
- DateTimeZone::listIdentifiers — すべてのタイムゾーン識別子を含む配列を返す
- DateInterval — DateInterval クラス
- DateInterval::__construct — 新しい DateInterval オブジェクトを作成する
- DateInterval::createFromDateString — 相対指定文字列から DateInterval を作成する
- DateInterval::format — 間隔をフォーマットする
- DatePeriod — DatePeriod クラス
- DatePeriod::__construct — 新しいい DatePeriod オブジェクトを作成する
- 日付・時刻 関数
- checkdate — グレゴリオ暦の日付/時刻の妥当性を確認します
- date_add — DateTime::add のエイリアス
- date_create_from_format — DateTime::createFromFormat のエイリアス
- date_create — DateTime::__construct のエイリアス
- date_date_set — DateTime::setDate のエイリアス
- date_default_timezone_get — スクリプト中の日付/時刻関数で使用されるデフォルトタイムゾーンを取得する
- date_default_timezone_set — スクリプト中の日付/時刻関数で使用されるデフォルトタイムゾーンを設定する
- date_diff — DateTime::diff のエイリアス
- date_format — DateTime::format のエイリアス
- date_get_last_errors — DateTime::getLastErrors のエイリアス
- date_interval_create_from_date_string — DateInterval::createFromDateString のエイリアス
- date_interval_format — DateInterval::format のエイリアス
- date_isodate_set — DateTime::setISODate のエイリアス
- date_modify — DateTime::modify のエイリアス
- date_offset_get — DateTime::getOffset のエイリアス
- date_parse_from_format — 指定した書式でフォーマットされた日付についての情報を取得する
- date_parse — 指定した日付に関する詳細な情報を連想配列で返す
- date_sub — DateTime::sub のエイリアス
- date_sun_info — 日の出/日の入り時刻と薄明かり (twilight) の開始/終了時刻の情報を含む配列を返す
- date_sunrise — 指定した日付と場所についての日の出時刻を返す
- date_sunset — 指定した日付と場所についての日の入り時刻を返す
- date_time_set — DateTime::setTime のエイリアス
- date_timestamp_get — DateTime::getTimestamp のエイリアス
- date_timestamp_set — DateTime::setTimestamp のエイリアス
- date_timezone_get — DateTime::getTimezone のエイリアス
- date_timezone_set — DateTime::setTimezone のエイリアス
- date — ローカルの日付/時刻を書式化する
- getdate — 日付/時刻情報を取得する
- gettimeofday — 現在の時間を得る
- gmdate — GMT/UTC の日付/時刻を書式化する
- gmmktime — GMT 日付から Unix タイムスタンプを取得する
- gmstrftime — ロケールの設定に基づいて GMT/UTC 時刻/日付をフォーマットする
- idate — ローカルな時刻/日付を整数として整形する
- localtime — ローカルタイムを得る
- microtime — 現在の Unix タイムスタンプをマイクロ秒まで返す
- mktime — 日付を Unix のタイムスタンプとして取得する
- strftime — ロケールの設定に基づいてローカルな日付・時間をフォーマットする
- strptime — strftime が生成した日付/時刻をパースする
- strtotime — 英文形式の日付を Unix タイムスタンプに変換する
- time — 現在の Unix タイムスタンプを返す
- timezone_abbreviations_list — DateTimeZone::listAbbreviations のエイリアス
- timezone_identifiers_list — DateTimeZone::listIdentifiers のエイリアス
- timezone_location_get — DateTimeZone::getLocation のエイリアス
- timezone_name_from_abbr — 略称からタイムゾーン名を返す
- timezone_name_get — DateTimeZone::getName のエイリアス
- timezone_offset_get — DateTimeZone::getOffset のエイリアス
- timezone_open — DateTimeZone::__construct のエイリアス
- timezone_transitions_get — DateTimeZone::getTransitions のエイリアス
- timezone_version_get — timezonedb のバージョンを取得する
- サポートする日付と時刻の書式
- サポートされるタイムゾーンのリスト
rjstatic
20-Mar-2011 04:44
Moo0z0r
21-Jun-2010 05:58
I think it's important to mention with the DateTime class that if you're trying to create a system that should store UNIX timestamps in UTC/GMT, and then convert them to a desired custom time-zone when they need to be displayed, using the following code is a good idea:
<?php
date_default_timezone_set('UTC');
?>
Even if you use something like:
<?php
$date->setTimezone( new DateTimeZone('UTC') );
?>
... before you store the value, it doesn't seem to work because PHP is already trying to convert it to the default timezone.
kapoor_rajiv at hotmail dot com
11-Oct-2009 10:59
We can also get the submitted datetime (e.g. 2009-11-06 07:03:41) using the following:
<?php
$DateOfRequest = date("Y-m-d H:i:s", strtotime($_REQUEST["DateOfRequest"]));
?>
Or another good example of getting DateTime:
<?php
$DateOfRequest = date("Y-m-d H:i:s", mktime($_REQUEST["Hour"],$_REQUEST["Min"],$_REQUEST
["Sec"],$_REQUEST["Month"],$_REQUEST["Day"],$_REQUEST["Year"]));
?>
zoe at monkeehouse dot com
24-Oct-2008 09:52
Should you want to convert between HH:MM:SS and plain seconds like in MySQL, these functions should do the trick:
<?php
function time_to_sec($time) {
$hours = substr($time, 0, -6);
$minutes = substr($time, -5, 2);
$seconds = substr($time, -2);
return $hours * 3600 + $minutes * 60 + $seconds;
}
function sec_to_time($seconds) {
$hours = floor($seconds / 3600);
$minutes = floor($seconds % 3600 / 60);
$seconds = $seconds % 60;
return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
}
?>
