Konwertuj format daty w PHP
Próbuję przekonwertować datę z yyyy-mm-dd
Na dd-mm-yyyy
(ale nie w SQL); jednak nie wiem, jak funkcja date wymaga znacznika czasu i nie mogę uzyskać znacznika czasu z tego ciągu.
18 answers
Użycie strtotime()
i date()
:
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(zobacz dokumentację strtotime i date na stronie PHP.)
Zauważ, że było to szybkie rozwiązanie pierwotnego pytania. W przypadku bardziej rozbudowanych konwersji, naprawdę powinieneś używać DateTime
klasa do parsowania i formatowania: -)
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
2019-03-01 15:23:10
Jeśli chcesz uniknąć konwersji strtotime (na przykład, strtotime nie jest w stanie przetworzyć danych wejściowych) możesz użyć,
$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString);
$newDateString = $myDateTime->format('d-m-Y');
Lub równoważnie:
$newDateString = date_format(date_create_from_format('Y-m-d', $dateString), 'd-m-Y');
Najpierw nadajesz mu format $dateString. Następnie mówisz mu format, w którym chcesz mieć $newDateString.
Lub jeśli format źródłowy zawsze jest "Y-m-D" (yyyy-mm-dd), po prostu użyj DateTime :
<?php
$source = '2012-07-31';
$date = new DateTime($source);
echo $date->format('d.m.Y'); // 31.07.2012
echo $date->format('d-m-Y'); // 31-07-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
2019-03-01 15:26:22
Użycie:
implode('-', array_reverse(explode('-', $date)));
Bez przeliczania daty, Nie jestem pewien, czy będzie to miało duże znaczenie.
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
2019-03-01 15:25:04
$newDate = preg_replace("/(\d+)\D+(\d+)\D+(\d+)/","$3-$2-$1",$originalDate);
Ten kod działa dla każdego formatu daty.
Możesz zmienić kolejność zmiennych zastępczych, takich jak $3 - $1 - $2 ze względu na stary format daty.
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-12-20 08:10:20
Również inna niejasna możliwość:
$oldDate = '2010-03-20'
$arr = explode('-', $oldDate);
$newDate = $arr[2].'-'.$arr[1].'-'.$arr[0];
Nie wiem czy bym z niego skorzystał ale i tak:)
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-02-05 07:53:26
$timestamp = strtotime(your date variable);
$new_date = date('d-m-Y', $timestamp);
Więcej informacji znajduje się w dokumentacji dla strtotime
.
Lub jeszcze krócej:
$new_date = date('d-m-Y', strtotime(your date variable));
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
2019-03-01 15:24:22
Uwaga: ponieważ odpowiedź na ten post czasami jest upvoted, wróciłem uprzejmie proszę ludzi, żeby już tego nie popierali. Moja odpowiedź brzmi: starożytne, nie poprawne technicznie, a jest kilka lepszych zbliża się tutaj. Trzymam go tylko dla historycznych cele.
Chociaż dokumentacja źle opisuje funkcję strtotime, @rjmunro poprawnie poruszył ten problem w komentarzu: jest w ISO format daty "RRRR-MM-DD".
Również, mimo że moja funkcja Date_Converter może nadal działać, chciałbym chciałbym ostrzec, że poniżej mogą być nieprecyzyjne stwierdzenia, więc proszę nie lekceważ ich.
najczęściej głosowana odpowiedź jest nieprawidłowa!
Podręcznik PHP strtotime tutaj stwierdza, że "funkcja oczekuje, że otrzyma łańcuch zawierający angielski format daty". W rzeczywistości oznacza to, że oczekuje amerykańskiego formatu daty, takiego jak" m-d-Y " lub "m / d / Y".
Oznacza to, że data podana jako "Y-m-d" może zostać błędnie zinterpretowana przez strtotime
. Należy podać datę w oczekiwanym formacie.
Napisałem małą funkcję zwracającą daty w kilku formatach. Używać i modyfikować do woli. jeśli ktoś zmieni to w klasę, byłbym zadowolony, gdyby to było dzielone.
function Date_Converter($date, $locale = "br") {
# Exception
if (is_null($date))
$date = date("m/d/Y H:i:s");
# Let's go ahead and get a string date in case we've
# been given a Unix Time Stamp
if ($locale == "unix")
$date = date("m/d/Y H:i:s", $date);
# Separate Date from Time
$date = explode(" ", $date);
if ($locale == "br") {
# Separate d/m/Y from Date
$date[0] = explode("/", $date[0]);
# Rearrange Date into m/d/Y
$date[0] = $date[0][1] . "/" . $date[0][0] . "/" . $date[0][2];
}
# Return date in all formats
# US
$Return["datetime"]["us"] = implode(" ", $date);
$Return["date"]["us"] = $date[0];
# Universal
$Return["time"] = $date[1];
$Return["unix_datetime"] = strtotime($Return["datetime"]["us"]);
$Return["unix_date"] = strtotime($Return["date"]["us"]);
$Return["getdate"] = getdate($Return["unix_datetime"]);
# BR
$Return["datetime"]["br"] = date("d/m/Y H:i:s", $Return["unix_datetime"]);
$Return["date"]["br"] = date("d/m/Y", $Return["unix_date"]);
# Return
return $Return;
} # End Function
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
2019-03-01 15:29:19
Są dwa sposoby realizacji tego:
1.
$date = strtotime(date);
$new_date = date('d-m-Y', $date);
2.
$cls_date = new DateTime($date);
echo $cls_date->format('d-m-Y');
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
2019-03-01 15:31:26
Możesz spróbować strftime()
funkcja. Prosty przykład: strftime($time, '%d %m %Y');
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
2019-04-02 16:18:28
Użyj tej funkcji do konwersji z dowolnego formatu na dowolny format
function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
$date_aux = date_create_from_format($from_format, $date);
return date_format($date_aux,$to_format);
}
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
2015-09-29 21:22:07
Poniżej podany jest kod PHP do wygenerowania jutrzejszej daty za pomocą mktime()
i zmiany jej formatu na dd / mm / RRRR, a następnie wydruku za pomocą echo
.
$tomorrow = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
echo date("d", $tomorrow) . "/" . date("m", $tomorrow). "/" . date("Y", $tomorrow);
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-02-02 10:53:46
date('m/d/Y h:i:s a',strtotime($val['EventDateTime']));
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
2015-07-23 06:51:48
Do tej konkretnej konwersji możemy również użyć ciągu formatu.
$new = vsprintf('%3$s-%2$s-%1$s', explode('-', $old));
Oczywiście nie będzie to działać w przypadku wielu innych konwersji formatu daty, ale ponieważ w tym przypadku przestawiamy podciągi, jest to inny możliwy sposób.
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
2019-04-23 00:30:28
Możesz zmienić format używając date () i strtotime ().
$date = '9/18/2019';
Echo date ('d-m-y',strtotime ($date));
Wynik:
18-09-19
Możemy zmienić format zmieniając ( d-m-y).
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
2019-09-18 12:30:14
Użyj date_create
i date_format
function formatDate($input, $output){
$inputdate = date_create($input);
$output = date_format($inputdate, $output);
return $output;
}
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
2019-12-12 12:10:54
W PHP każda data może być przekonwertowana do wymaganego formatu daty przy użyciu różnych scenariuszy, na przykład, aby zmienić dowolny format daty NA Dzień, Data Miesiąc Rok
$newdate = date("D, d M Y", strtotime($date));
Pokaże datę w następującym bardzo dobrym formacie
Pon, 16 Lis 2020]}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
2020-11-19 06:53:22
Prosty sposób użycia strtotime()
i date()
:
$original_dateTime = "2019-05-11 17:02:07"; #This may be database datetime
$newDate = date("d-m-Y", strtotime($original_dateTime));
Z czasem
$newDate = date("d-m-Y h:i:s a", strtotime($original_dateTime));
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
2019-05-11 12:21:39
function dateFormat($date)
{
$m = preg_replace('/[^0-9]/', '', $date);
if (preg_match_all('/\d{2}+/', $m, $r)) {
$r = reset($r);
if (count($r) == 4) {
if ($r[2] <= 12 && $r[3] <= 31) return "$r[0]$r[1]-$r[2]-$r[3]"; // Y-m-d
if ($r[0] <= 31 && $r[1] != 0 && $r[1] <= 12) return "$r[2]$r[3]-$r[1]-$r[0]"; // d-m-Y
if ($r[0] <= 12 && $r[1] <= 31) return "$r[2]$r[3]-$r[0]-$r[1]"; // m-d-Y
if ($r[2] <= 31 && $r[3] <= 12) return "$r[0]$r[1]-$r[3]-$r[2]"; //Y-m-d
}
$y = $r[2] >= 0 && $r[2] <= date('y') ? date('y') . $r[2] : (date('y') - 1) . $r[2];
if ($r[0] <= 31 && $r[1] != 0 && $r[1] <= 12) return "$y-$r[1]-$r[0]"; // d-m-y
}
}
var_dump(dateFormat('31/01/00')); // return 2000-01-31
var_dump(dateFormat('31/01/2000')); // return 2000-01-31
var_dump(dateFormat('01-31-2000')); // return 2000-01-31
var_dump(dateFormat('2000-31-01')); // return 2000-01-31
var_dump(dateFormat('20003101')); // return 2000-01-31
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
2020-06-19 21:01:46