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"
?>
Date and Time
- 简介
- 安装/配置
- 预定义常量
- DateTime — The DateTime class
- DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
- DateTime::__construct — Returns new DateTime object
- DateTime::createFromFormat — Returns new DateTime object formatted according to the specified format
- DateTime::getLastErrors — Returns the warnings and errors
- DateTime::modify — Alters the timestamp
- DateTime::__set_state — The __set_state handler
- DateTime::setDate — Sets the date
- DateTime::setISODate — Sets the ISO date
- DateTime::setTime — Sets the time
- DateTime::setTimestamp — Sets the date and time based on an Unix timestamp
- DateTime::setTimezone — Sets the time zone for the DateTime object
- DateTime::sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
- DateTimeImmutable — The DateTimeImmutable class
- DateTimeImmutable::add — Adds an amount of days, months, years, hours, minutes and seconds
- DateTimeImmutable::__construct — Returns new DateTimeImmutable object
- DateTimeImmutable::createFromFormat — Returns new DateTimeImmutable object formatted according to the specified format
- DateTimeImmutable::getLastErrors — Returns the warnings and errors
- DateTimeImmutable::modify — Alters the timestamp
- DateTimeImmutable::__set_state — The __set_state handler
- DateTimeImmutable::setDate — Sets the date
- DateTimeImmutable::setISODate — Sets the ISO date
- DateTimeImmutable::setTime — Sets the time
- DateTimeImmutable::setTimestamp — Sets the date and time based on an Unix timestamp
- DateTimeImmutable::setTimezone — Sets the time zone
- DateTimeImmutable::sub — Subtracts an amount of days, months, years, hours, minutes and seconds
- DateTimeInterface — The DateTimeInterface interface
- DateTime::diff — Returns the difference between two DateTime objects
- DateTime::format — Returns date formatted according to given format
- DateTime::getOffset — Returns the timezone offset
- DateTime::getTimestamp — Gets the Unix timestamp
- DateTime::getTimezone — Return time zone relative to given DateTime
- DateTime::__wakeup — The __wakeup handler
- DateTimeZone — The DateTimeZone class
- DateTimeZone::__construct — Creates new DateTimeZone object
- DateTimeZone::getLocation — Returns location information for a timezone
- DateTimeZone::getName — Returns the name of the timezone
- DateTimeZone::getOffset — Returns the timezone offset from GMT
- DateTimeZone::getTransitions — Returns all transitions for the timezone
- DateTimeZone::listAbbreviations — Returns associative array containing dst, offset and the timezone name
- DateTimeZone::listIdentifiers — Returns numerically index array with all timezone identifiers
- DateInterval — The DateInterval class
- DateInterval::__construct — Creates a new DateInterval object
- DateInterval::createFromDateString — Sets up a DateInterval from the relative parts of the string
- DateInterval::format — Formats the interval
- DatePeriod — The DatePeriod class
- DatePeriod::__construct — Creates a new DatePeriod object
- Date/Time 函数
- checkdate — 验证一个格里高里日期
- date_add — 别名 DateTime::add
- date_create_from_format — 别名 DateTime::createFromFormat
- date_create_immutable_from_format — 别名 DateTimeImmutable::createFromFormat
- date_create_immutable — 别名 DateTimeImmutable::__construct
- 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 — Get info about given date formatted according to the specified format
- date_parse — Returns associative array with detailed info about given date
- date_sub — 别名 DateTime::sub
- date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end
- 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 — Returns the timezone name from abbreviation
- timezone_name_get — 别名 DateTimeZone::getName
- timezone_offset_get — 别名 DateTimeZone::getOffset
- timezone_open — 别名 DateTimeZone::__construct
- timezone_transitions_get — 别名 DateTimeZone::getTransitions
- timezone_version_get — Gets the version of the timezonedb
- Supported Date and Time Formats
- 所支持的时区列表
rjstatic ¶
2 years ago
Moo0z0r ¶
2 years ago
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 ¶
3 years ago
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 ¶
4 years ago
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);
}
?>
