Skip to content

MySQL DATETIME Helper

World Wide Web Server edited this page Jul 4, 2012 · 12 revisions

To enhance CI's date helper with MySQL DATETIME / DATE functionality just create date_helper.php in your application’s helpers directory, paste the original date_helper.php in (to keep its functionality) and then add the following code:

/**

  • Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to timestamp

  • Returns the timestamp equivalent of a given DATE/DATETIME

  • @todo add regex to validate given datetime

  • @author Clemens Kofler clemens.kofler@chello.at

  • @access public

  • @return integer */ function mysqldatetime_to_timestamp($datetime = "") { // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters) $l = strlen($datetime); if(!($l == 10 || $l == 19)) return 0;

    // $date = $datetime; $hours = 0; $minutes = 0; $seconds = 0;

    // DATETIME only if($l == 19) { list($date, $time) = explode(" ", $datetime); list($hours, $minutes, $seconds) = explode(":", $time); }

    list($year, $month, $day) = explode("-", $date);

    return mktime($hours, $minutes, $seconds, $month, $day, $year); }

/**

  • Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to date using given format string
  • Returns the date (format according to given string) of a given DATE/DATETIME
  • @author Clemens Kofler clemens.kofler@chello.at
  • @access public
  • @return integer */ function mysqldatetime_to_date($datetime = "", $format = "d.m.Y, H:i:s") { return date($format, mysqldatetime_to_timestamp($datetime)); }

/**

  • Convert timestamp to MySQL's DATE or DATETIME (YYYY-MM-DD hh:mm:ss)

  • Returns the DATE or DATETIME equivalent of a given timestamp

  • @author Clemens Kofler clemens.kofler@chello.at

  • @access public

  • @return integer */ function timestamp_to_mysqldatetime($timestamp = "", $datetime = true) { if(empty($timestamp) || !is_numeric($timestamp)) $timestamp = time();

    return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp); }

Clone this wiki locally