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"
?>
Data e Hora
- Introdução
- Instalação/Configuração
- Constantes pré-definidas
- Lista de Timezones Suportados
- Funções de Data/Hora
- checkdate — Valida uma data Gregoriana
- date_add — Sinônimo de DateTime::add
- date_create_from_format — Sinônimo de DateTime::createFromFormat
- date_create — Retorna um novo objeto DateTime
- date_date_set — Define a data
- date_default_timezone_get — Retorna a timezone (zona de tempo) padrão usada por todas as funções de data e tempo em um script
- date_default_timezone_set — Configura a timezone padrão a ser utilizada por todas as funções de data e hora em um script
- date_diff — Sinônimo de DateTime::diff
- date_format — Retorna a data formatada de acordo com o formato dado
- date_get_last_errors — Sinônimo de DateTime::getLastErrors
- date_interval_create_from_date_string — Sinônimo de DateInterval::createFromDateString
- date_interval_format — Sinônimo de DateInterval::format
- date_isodate_set — Define a data ISO
- date_modify — Altera o timestamp
- date_offset_get — Sinônimo de DateTime::getOffset
- date_parse_from_format — Get info about given date formatted according to the specified format
- date_parse — Retorna um array associativo com detalhes sobre uma dada data
- date_sub — Sinônimo de DateTime::sub
- date_sun_info — Retorna um array com informações sobre pôr-do-sol/nascer-do-sol e o início/fim do dia
- date_sunrise — Returns time of sunrise for a given day and location
- date_sunset — Returns time of sunset for a given day and location
- date_time_set — Define o tempo
- date_timestamp_get — Sinônimo de DateTime::getTimestamp
- date_timestamp_set — Sinônimo de DateTime::setTimestamp
- date_timezone_get — Sinônimo de DateTime::getTimezone
- date_timezone_set — Sinônimo de DateTime::setTimezone
- date — Formata a data e a hora local
- getdate — Consegue informações data/hora
- gettimeofday — Obtém a hora local
- gmdate — Formata uma data/hora GMT/CUT
- gmmktime — Obtém um timestamp Unix para uma data GMT
- gmstrftime — Formata uma hora/data GMT/CUT de acordo com as configurações locais
- idate — Format a local time/date as integer
- localtime — Obtém a hora local
- microtime — Retorna um timestamp Unix com microsegundos
- mktime — Obtém um timestamp Unix para uma data
- strftime — Formata uma hora/data de acordo com as configurações locais
- strptime — Parse a time/date generated with strftime
- strtotime — Analisa qualquer descrição em texto em inglês de data hora em timestamp Unix
- time — Retorna o timestamp Unix atual
- timezone_abbreviations_list — Sinônimo de DateTimeZone::listAbbreviations
- timezone_identifiers_list — Sinônimo de DateTimeZone::listIdentifiers
- timezone_location_get — Sinônimo de DateTimeZone::getLocation
- timezone_name_from_abbr — Returns the timezone name from abbreviation
- timezone_name_get — Sinônimo de DateTimeZone::getName
- timezone_offset_get — Sinônimo de DateTimeZone::getOffset
- timezone_open — Retorna um novo objeto DateTimeZone
- timezone_transitions_get — Sinônimo de DateTimeZone::getTransitions
- timezone_version_get — Gets the version of the timezonedb
rjstatic
20-Mar-2011 10:44
Moo0z0r
21-Jun-2010 11: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
12-Oct-2009 04: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 03: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);
}
?>
