Posted By

dubogii on 05/17/11


Tagged

form date format conversion


Versions (?)

Change Date from dd/mm/yyyy to yyyy-dd-mm


 / Published in: PHP
 

URL: http://css-tricks.com/snippets/php/change-date-from-ddmmyyyy-to-yyyy-dd-mm/

  1. Conversion
  2.  
  3. $date_array = explode("/",$date); // split the array
  4. $var_day = $date_array[0]; //day seqment
  5. $var_month = $date_array[1]; //month segment
  6. $var_year = $date_array[2]; //year segment
  7. $new_date_format = "$var_year-$var_day-$var_month"; // join them together
  8.  
  9.  
  10.  
  11. Possibly a more MySQL friendly format in some circumstances.
  12. Change period-separated to slash-separated or vice versa (and reverse order)
  13.  
  14. Convert date from YYYY/MM/DD to DD.MM.YYYY (and from DD.MM.YYYY to YYYY/MM/DD)
  15.  
  16. /**
  17.  * @param string $date (d.m.y, y-m-d, y/m/d)
  18.  * @return string|bol
  19.  */
  20.  
  21. function convertDate($date) {
  22. // EN-Date to GE-Date
  23. if (strstr($date, "-") || strstr($date, "/")) {
  24. $date = preg_split("/[\/]|[-]+/", $date);
  25. $date = $date[2].".".$date[1].".".$date[0];
  26. return $date;
  27. }
  28. // GE-Date to EN-Date
  29. else if (strstr($date, ".")) {
  30. $date = preg_split("[.]", $date);
  31. $date = $date[2]."-".$date[1]."-".$date[0];
  32. return $date;
  33. }
  34. return false;
  35. }

Report this snippet  

You need to login to post a comment.