PHP date format date dd / mm / yyyy => yyyy-mm-dd

Possible duplicate:
PHP date string format

I am trying to convert a date from dd/mm/yyyy => yyyy-mm-dd . I have a mktime () function and other functions, but I cannot get it to work. I removed explode original date using '/' as a separator, but I was unable to change the format and replace '/' with '-' .

Any help would be greatly appreciated.

+73
date php formatting mktime
Apr 24 2018-12-12T00:
source share
5 answers

Dates in m/d/y or dmy are eliminated by searching on the separator between the various components: if the separator is slash ( / ), then the American m/d/y assumed; whereas if the separator is a dash ( - ) or a period ( . ), then the European dmy format. Check here .

Use the default date function.

 $var = "20/04/2012"; echo date("Ymd", strtotime($var) ); 

EDIT I just tested it and somehow PHP is not working with the dd / mm / yyyy format. Here is another solution.

 $var = '20/04/2012'; $date = str_replace('/', '-', $var); echo date('Ym-d', strtotime($date)); 
+214
Apr 24 2018-12-12T00:
source share

Try using DateTime::createFromFormat

 $date = DateTime::createFromFormat('d/m/Y', "24/04/2012"); echo $date->format('Ym-d'); 

Exit

 2012-04-24 

EDIT:

If the date is 5/4/2010 (both D / M / YYYY or DD / MM / YYYY), this method is used below to convert 5/4/2010 to 2010-4-5 (both: YYYY-MM-DD or YYYY-MD) format.

 $old_date = explode('/', '5/4/2010'); $new_data = $old_date[2].'-'.$old_date[1].'-'.$old_date[0]; 

EXIT:

 2010-4-5 
+83
Apr 24 2018-12-12T00:
source share

Here's another solution not using date (). not very smart :)

 $var = '20/04/2012'; echo implode("-", array_reverse(explode("/", $var))); 
+30
Apr 24 2018-12-12T00:
source share

Do it:

 date('Ym-d', strtotime('dd/mm/yyyy')); 

But make sure 'dd / mm / yyyy' is the actual date.

+3
Apr 24 2018-12-12T00:
source share

I see excellent answers, so there is no need to repeat here, so I would like to offer some tips:

I would recommend using an integer Unix Timestamp value instead of a human-readable date format for internal time processing, and then use the PHP date() function to convert the timestamp value to a human-readable date format for display by the user. Here is a rough example of how this should be done:

 // Get unix timestamp in seconds $current_time = date(); // Or if you need millisecond precision // Get unix timestamp in milliseconds $current_time = microtime(true); 

Then use $current_time as needed in your application (save, add or subtract, etc.), and then when you need to display the date value for your users, you can use date() to specify the desired date format:

 // Display a human-readable date format echo date('dm-Y', $current_time); 

Thus, you will avoid the headache associated with date formats, conversions and time zones, since your dates will be in a standardized format (Unix Timestamp), which is compact, independent of the time zone (always in UTC) and widely supported in programming languages ​​and Databases

+1
Apr 24 2018-12-12T00:
source share



All Articles