Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Parses the given datetime string and changes it to the given format. This uses strtotime()
- * to convert the datetime to timestamp, then changes format using date()
- *
- * @param string $dateStr String representing the datetime. If empty current datetime is used.
- * @param string $format The desired format. Default: d/m/Y
- * @param string $defaultIfInvalid A value to be returned if the given datetime is invalid
- *
- * @return string
- */
- public static function formatDate($dateStr = '',$format = 'd/m/Y',$defaultIfInvalid = '-') {
- $dateStr = trim($dateStr);
- if(!$dateStr) {
- $dateStr = date( 'Y-m-d H:i:s' );
- }
- $format = trim($format);
- if(!$format) {
- $format = 'd/m/Y';
- }
- $ts = strtotime($dateStr);
- if(!$ts) {
- return $defaultIfInvalid;
- }
- return date($format,$ts);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement