The dateFormat function is one that I love. Throw any kind of date format at it, request any format back, it just works.
Examples of usage:
$humanDisplay = dateFormat('2013-08-22 17:52:33','human'); $tomorrow = dateFormat('now + 1 day','human'); $mysql_date = dateFormat('8/30/2013','mysqldate'); $mysql_datetime = dateFormat('8/30/2013 5:00 PM','mysql'); $unix_timestamp = dateFormat('8/30/2013 5:00 PM','php'); if(dateFormat('now','php')>dateFormat($someVariable,'php')) { // do stuff }
The dateFormat function (and related supporting functions):
function parseTimezone($inputDate) { if( strpos($inputDate,'Eastern')>0 || strpos($inputDate,'Central')>0 || strpos($inputDate,'Mountain')>0 || strpos($inputDate,'Pacific')>0 || strpos($inputDate,'Alaska')>0 || strpos($inputDate,'Hawaii-Aleutian')>0 || strpos($inputDate,'Arizona')>0 || strpos($inputDate,'America/')>0 || strpos($inputDate,'Pacific/')>0 || strpos($inputDate,'GMT')>0 ) { $parts = explode(' ',$inputDate); $timezoneName = array_pop($parts); $inputDate = implode(' ',$parts); } return $inputDate; } function isValidDate($inputDate) { if($inputDate=="01/01/0001" || $inputDate=="01/01/01" || $inputDate=="0001-01-01" || $inputDate=="0000-01-01" || $inputDate=="0000-00-00" || $inputDate=="" || strlen($inputDate)<3 ) { return false; // don't increase the required length by more than 3 so "now" doesn't get cut } $inputDate = parseTimezone($inputDate); if(is_numeric($inputDate) && $inputDate>100000000) { // 100000000 = 1973-03-03 01:46:40 // this should catch all integers that are errantly caught as dates $phpTime = $inputDate; } elseif($inputDate!='' && ( strpos(strtolower($inputDate),'now')!==false || strpos($inputDate,'/') || strpos($inputDate,'-') || strpos($inputDate,'day') || strpos($inputDate,'week') || strpos($inputDate,'month') || strpos($inputDate,'year') ) ) { $phpTime = strtotime($inputDate); } else { return false; } if($phpTime==-62135568000) { // 0000-00-00 return false; } if($phpTime==NULL) { return false; } $month = date('n',$phpTime); $day = date('j',$phpTime); $year = date('Y',$phpTime); return checkdate($month,$day,$year); } function dateFormat($inputDate, $format=NULL, $params=array()) { if(!isValidDate($inputDate)) { $inputDate=NULL; } if(strtolower($inputDate)=="now" || strtolower($inputDate)=="now()") { $inputDate = "now"; } if($format==NULL) { $format="human"; } $inputDate = parseTimezone($inputDate); if(is_numeric($inputDate) && (int)$inputDate==$inputDate) { $phpTime = $inputDate; } elseif($inputDate!='') { $phpTime = strtotime($inputDate); } if($phpTime) { if($format=="human") { return date("m/d/Y",$phpTime); } else if(strtolower($format)=="mysql") { return date("Y-m-d H:i:s",$phpTime); } else if(strtolower($format)=="mysqldate" || ($format=="%Q") ) { return date("Y-m-d",$phpTime); } else if(strtolower($format)=="mysqltime" || ($format=="%T") ) { return date("H:i:s",$phpTime); } else if(strtolower($format)=="solr") { return gmdate('Y-m-d\TH:i:s\Z',$phpTime); } else if(strtolower($format)=="php") { return $phpTime; } else if(strtolower($format)=="human datetime") { return date("n/j/y g:i A",$phpTime); } else if($format!='') { return date($format,$phpTime); } } else { return NULL; } }
Michael Berding September 13th, 2013
Posted In: Functions