Thursday, September 25, 2014

Convert MySQl date format to PHP, convert date to term




In MySQl, the date format is yyyy-m-d.
To change to date format in PH, $dateMySQL below is MySQl date format  yyyy-m-d.
      $date = strtotime($dateMySQL);
        $year = date("Y", $date );
        $month = date("n", $date);
        $day = date("j", $date );


$year, $month, $day is to extract year, month and day. strtotime is to convert format time to to unix time.
Below is an example to convert MySQL date format to SFU term. and return the style of box shadow
in red.
    function style_for_initialcredit($termkey, $datecredit) {
        $date = strtotime($datecredit);
        $year = date("Y", $date );
        $month = date("n", $date);
        $day = date("j", $date );
        if($month>=1 && $month<=4) {$term  = ($year-1900)*10+1;}
        elseif($month>=5 && $month<=8) {$term  = ($year-1900)*10+4;}
        elseif($month>=9 && $month<=12) {$term  = ($year-1900)*10+7;}
        else  $term = 0;       

        $style = "";
        if($term==$termkey)
            $style .=" box-shadow: inset -10px 0 5px -5px hsla(0,100%,50%,0.5); ";
        return trim($style);
  
    }

No comments:

Post a Comment