PHP Konwertuj datetime NA UTC
Potrzebuję łatwego sposobu na konwersję znacznika czasu daty NA UTC (z dowolnej strefy czasowej serwera), mam nadzieję, że bez użycia bibliotek.
14 answers
Spróbuj getTimezone i setTimezone , Zobacz przykład
(Ale To używa klasy)
Aktualizacja:
Bez żadnych klas można spróbować czegoś takiego:
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
Uwaga: Może być konieczne ustawienie strefy czasowej z powrotem do oryginału
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-19 18:54:59
Użyj strtotime aby wygenerować znacznik czasu z podanego ciągu znaków (interpretowanego jako czas lokalny) i użyj gmdate aby uzyskać go jako sformatowaną datę UTC wstecz.
Przykład
Zgodnie z życzeniem, Oto prosty przykład:
echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-28 21:56:20
Użycie DateTime:
$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 07:24:33
Zrób tak:
gmdate('Y-m-d H:i:s', $timestamp)
Lub po prostu
gmdate('Y-m-d H:i:s')
Aby uzyskać "TERAZ" W UTC.
Sprawdź odniesienie:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-01 13:31:08
Jeśli masz datę w tym formacie RRRR-MM-HH dd: mm: ss, możesz rzeczywiście oszukać php, dodając UTC na końcu "łańcucha datetime" i użyć strtotime do konwersji go.
date_default_timezone_set('Europe/Stockholm');
print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";
To wydrukuje to:
2009-01-01 13:00:00
2009-06-01 14:00:00
I jak widać, zajmuje się również problemem czasu letniego.
Trochę dziwny sposób na rozwiązanie tego problemu.... :)Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-14 21:50:01
Ponieważ strtotime wymaga określonego formatu wejściowego, można użyć DateTime::createFromFormat (wymagane jest PHP 5.3 + )
// set timezone to user timezone
date_default_timezone_set($str_user_timezone);
// create date object using any given format
$date = DateTime::createFromFormat($str_user_dateformat, $str_user_datetime);
// convert given datetime to safe format for strtotime
$str_user_datetime = $date->format('Y-m-d H:i:s');
// convert to UTC
$str_UTC_datetime = gmdate($str_server_dateformat, strtotime($str_user_datetime));
// return timezone to server default
date_default_timezone_set($str_server_timezone);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-18 19:17:29
Kiedyś używam tej metody:
// It is not importnat what timezone your system is set to.
// Get the UTC offset in seconds:
$offset = date("Z");
// Then subtract if from your original timestamp:
$utc_time = date("Y-m-d H:i:s", strtotime($original_time." -".$offset." Seconds"));
Działa Wszystkie większość czasu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-27 13:33:19
Konwertuj łańcuch lokalnej strefy czasowej na łańcuch UTC.
np. Strefa czasowa Nowej Zelandii
$datetime = "2016-02-01 00:00:01";
$given = new DateTime($datetime, new DateTimeZone("Pacific/Auckland"));
$given->setTimezone(new DateTimeZone("UTC"));
$output = $given->format("Y-m-d H:i:s");
echo ($output);
- NZDT: UTC + 13: 00
if $datetime = "2016-02-01 00:00:01", $wyjście = "2016-01-31 11:00:01";
if $datetime = "2016-02-29 23:59:59", $wyjście = "2016-02-29 10:59:59";
- NZST: UTC + 12: 00
if $datetime = "2016-05-01 00:00:01", $wyjście = "2016-04-30 12:00:01";
if $datetime = "2016-05-31 23:59:59", $output = "2016-05-31 11:59:59";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-04 05:32:43
Http://php.net/manual/en/function.strtotime.php lub jeśli nie musisz używać ciągu znaków, ale składników czasu, to http://us.php.net/manual/en/function.mktime.php
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-19 18:06:54
W PHP 5 lub superior możesz użyć funkcji datetime:: format (patrz dokumentacja http://us.php.net/manual/en/datetime.format.php )
echo strftime( '%e %B %Y' ,
date_create_from_format('Y-d-m G:i:s', '2012-04-05 11:55:21')->format('U')
); // 4 May 2012
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-09 11:05:48
Try
Echo date ('F D Y', strtotime('2010-01-19 00:00:00'));
Wyświetli:
Styczeń 19 2010
Powinieneś zmienić czas formatowania, aby zobaczyć inne wyjście
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-18 16:42:10
Funkcja normalizacji ogólnego przeznaczenia do formatowania dowolnego znacznika czasu z dowolnej strefy czasowej na inną.
Bardzo przydatne do przechowywania datetimestamps użytkowników z różnych stref czasowych w relacyjnej bazie danych. Dla porównań baz danych przechowuj znacznik czasu jako UTC i używaj z gmdate('Y-m-d H:i:s')
/**
* Convert Datetime from any given olsonzone to other.
* @return datetime in user specified format
*/
function datetimeconv($datetime, $from, $to)
{
try {
if ($from['localeFormat'] != 'Y-m-d H:i:s') {
$datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Y-m-d H:i:s');
}
$datetime = new DateTime($datetime, new DateTimeZone($from['olsonZone']));
$datetime->setTimeZone(new DateTimeZone($to['olsonZone']));
return $datetime->format($to['localeFormat']);
} catch (\Exception $e) {
return null;
}
}
użycie:
$from = ['localeFormat' => "d/m/Y H:i A", 'olsonZone' => 'Asia/Calcutta']; $to = ['localeFormat' => "Y-m-d H:i:s", 'olsonZone' => 'UTC']; datetimeconv("14/05/1986 10:45 PM", $from, $to); // returns "1986-05-14 17:15:00"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-09 06:03:34
Jako ulepszenie na odpowiedź Philla Pafforda (nie zrozumiałem jego 'Y-d-mTG:i:sz' i zasugerował przywrócenie strefy czasowej). Więc proponuję to (skomplikowałem zmieniając format HMTL w zwykły / tekst...):
<?php
header('content-type: text/plain;');
$my_timestamp = strtotime("2010-01-19 00:00:00");
// stores timezone
$my_timezone = date_default_timezone_get();
echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date)\n";
// changes timezone
date_default_timezone_set("UTC");
echo date("Y-m-d\TH:i:s\Z", $my_timestamp)."\t\t (ISO8601 UTC date)\n";
echo date("Y-m-d H:i:s", $my_timestamp)."\t\t (your UTC date)\n";
// reverts change
date_default_timezone_set($my_timezone);
echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date is back)\n";
?>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-11 19:36:29
Alternatywnie możesz spróbować tego:
<?php echo (new DateTime("now", new DateTimeZone('Asia/Singapore')))->format("Y-m-d H:i:s e"); ?>
Wyświetli się :
2017-10-25 17:13:20 Azja / Singapur
Możesz użyć tego wewnątrz atrybutu value w polu wprowadzania tekstu, jeśli chcesz wyświetlić tylko datę tylko do odczytu.
Usuń "e", jeśli nie chcesz pokazywać swojego regionu / kraju.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-25 09:16:33