Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Parses the given datetime string and changes it to the given format. This uses strtotime()
  5. * to convert the datetime to timestamp, then changes format using date()
  6. *
  7. * @param string $dateStr String representing the datetime. If empty current datetime is used.
  8. * @param string $format The desired format. Default: d/m/Y
  9. * @param string $defaultIfInvalid A value to be returned if the given datetime is invalid
  10. *
  11. * @return string
  12. */
  13.  
  14. public static function formatDate($dateStr = '',$format = 'd/m/Y',$defaultIfInvalid = '-') {
  15.  
  16. $dateStr = trim($dateStr);
  17.  
  18. if(!$dateStr) {
  19. $dateStr = date( 'Y-m-d H:i:s' );
  20. }
  21.  
  22. $format = trim($format);
  23.  
  24. if(!$format) {
  25. $format = 'd/m/Y';
  26. }
  27.  
  28. $ts = strtotime($dateStr);
  29.  
  30. if(!$ts) {
  31. return $defaultIfInvalid;
  32. }
  33.  
  34. return date($format,$ts);
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement