Convert PHP date() style dateFormat to the equivalent jQuery UI datepicker string


/ Published in: PHP
Save to your folder(s)

This is a simple bit of code that allows you to do a write once date format in a PHP style date() format and convert it to be used by datepicker jQuery UI widget. It returns the converted string.\r\n\r\nNothing fancy here, but a time saver for the future.\r\n\r\nCould use improvement such as error handling for common PHP dateFormat string values that do not have an equivalent UI portion. Potentially make this a two way conversion. Add more available options.


Copy this code and paste it in your HTML
  1. function dateStringToDatepickerFormat($dateString)
  2. {
  3. $pattern = array(
  4.  
  5. //day
  6. 'd', //day of the month
  7. 'j', //3 letter name of the day
  8. 'l', //full name of the day
  9. 'z', //day of the year
  10.  
  11. //month
  12. 'F', //Month name full
  13. 'M', //Month name short
  14. 'n', //numeric month no leading zeros
  15. 'm', //numeric month leading zeros
  16.  
  17. //year
  18. 'Y', //full numeric year
  19. 'y' //numeric year: 2 digit
  20. );
  21. $replace = array(
  22. 'dd','d','DD','o',
  23. 'MM','M','m','mm',
  24. 'yy','y'
  25. );
  26. foreach($pattern as &$p)
  27. {
  28. $p = '/'.$p.'/';
  29. }
  30. return preg_replace($pattern,$replace,$dateString);
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.