New formatting date and time in php, convert a date format in php

I am trying lớn convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL); however I don"t know how the date function requires a timestamp, và I can"t get a timestamp from this string.

Bạn đang xem: New formatting date and time in php, convert a date format in php

Bạn đã xem: Formatting date and time

How is this possible?



Use strtotime() & date():

Note that this was a quick solution khổng lồ the original question. For more extensive conversions, you should really be using the DateTime class lớn parse and format :-)


If you"d lượt thích to avoid the strtotime conversion (for example, strtotime is not being able to lớn parse your input) you can use,

$myDateTime = DateTime::createFromFormat("Y-m-d", $dateString);$newDateString = $myDateTime->format("d-m-Y");Or, equivalently:

$newDateString = date_format(date_create_from_format("Y-m-d", $dateString), "d-m-Y");You are first giving it the format $dateString is in. Then you are telling it the format you want $newDateString khổng lồ be in.

Or if the source-format always is "Y-m-d" (yyyy-mm-dd), then just use DateTime:

nội dung Follow edited Mar 1 "19 at 15:26

Peter Mortensen 27.7k2121 gold badges9494 silver badges123123 bronze badges answered Jul 11 "12 at 15:06 ceiroaceiroa 5,33322 gold badges1919 silver badges1818 bronze badges 5 showroom a comment | 74Use:

implode("-", array_reverse(explode("-", $date)));Without the date conversion overhead, I am not sure it"ll matter much.

cốt truyện Follow edited Mar 1 "19 at 15:25 Peter Mortensen 27.7k2121 gold badges9494 silver badges123123 bronze badges answered Mar 21 "10 at 18:23 Tim LytleTim Lytle 16.9k99 gold badges5959 silver badges8888 bronze badges 10 | Show 5 more comments 39$newDate = preg_replace("/(d+)D+(d+)D+(d+)/","$3-$2-$1",$originalDate);This code works for every date format.

You can change the order of replacement variables such $3-$1-$2 due lớn your old date format.

tóm tắt Follow edited Dec đôi mươi "11 at 8:10 answered Dec 19 "11 at 13:47 Alper SarıAlper Sarı 39133 silver badges44 bronze badges 1 add a phản hồi | 28Also another obscure possibility: tóm tắt Follow edited Feb 5 "16 at 7:53 Finduilas 70011 gold badge1010 silver badges2727 bronze badges answered Mar 21 "10 at 18:16 GabrielGabriel 1,69022 gold badges2222 silver badges2929 bronze badges 1 showroom a phản hồi | 28$timestamp = strtotime(your date variable); $new_date = date("d-m-Y", $timestamp);For more, see the documentation for strtotime.

Xem thêm: Cách Tắt Nhạc Nền Liên Minh Huyền Thoại, Nhạc Nền Liên Minh Huyền Thoại

Or even shorter:

Although the documentation poorly describes the strtotime function, rjmunro correctly addressed the issue in his comment: it"s in ISO format date "YYYY-MM-DD".

Also, even though my Date_Converter function might still work, I"d lượt thích to warn that there may be imprecise statements below, so please vì disregard them.

The most voted answer is actually incorrect!

The PHP strtotime manual here states that "The function expects lớn be given a string containing an English date format". What it actually means is that it expects an American US date format, such as "m-d-Y" or "m/d/Y".

That means that a date provided as "Y-m-d" may get misinterpreted by strtotime. You should provide the date in the expected format.

I wrote a little function to return dates in several formats. Use and modify at will. If anyone does turn that into a class, I"d be glad if that would be shared.

function Date_Converter($date, $locale = "br") # Exception if (is_null($date)) $date = date("m/d/Y H:i:s"); # Let"s go ahead & 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 = explode("/", $date); # Rearrange Date into m/d/Y $date = $date . "/" . $date . "/" . $date; # Return date in all formats # US $Return = implode(" ", $date); $Return = $date; # Universal $Return = $date; $Return = strtotime($Return); $Return = strtotime($Return); $Return = getdate($Return); # BR $Return = date("d/m/Y H:i:s", $Return); $Return = date("d/m/Y", $Return); # Return return $Return; # end Function