#native_company# #native_desc#
#native_cta#

Date conversion

By Jeff
on June 15, 2005

For the most part anything I code I need to convert between mysql date style, U.S. date style, and unix timestamp. There are a lot of date conversion functions out there but anything I usually do up and beyond this simple conversion function is pretty specific to the application.
I always validate my dates before I submit for conversion.

In any case, I thought I would share this with you all.

$date="12/12/2005";
if ($converted_date=date_conversion($date)){
     print $converted_date->unix_timestamp;
     exit;
}else{
     print "Date format is not recognized";
     exit;
}

function date_conversion($date){
     if(eregi("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$date) ||
        eregi("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})",$date)){
         $the_date=strtotime($date);
     }elseif(is_int($date) && $date>0){//is a probably a unix date
         $the_date=$date;
     }else{
         return false;//date format not recognized
     }
     $return_date->unix_timestamp=$the_date;
     $return_date->human_date=date("m/d/Y",$the_date);
     $return_date->mysql_date=date("Y-m-d",$the_date);
     return $return_date;
}